PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/json.c

https://code.google.com/
C | 133 lines | 108 code | 2 blank | 23 comment | 2 complexity | 4fde312ea8ebe6e5cb83f1913d8bbf1f MD5 | raw file
  1/*
  2    $Id: json.c 231 2011-06-27 13:46:19Z marc.noirot $
  3
  4    FLV Metadata updater
  5
  6    Copyright (C) 2007-2012 Marc Noirot <marc.noirot AT gmail.com>
  7
  8    This file is part of FLVMeta.
  9
 10    FLVMeta is free software; you can redistribute it and/or modify
 11    it under the terms of the GNU General Public License as published by
 12    the Free Software Foundation; either version 2 of the License, or
 13    (at your option) any later version.
 14
 15    FLVMeta is distributed in the hope that it will be useful,
 16    but WITHOUT ANY WARRANTY; without even the implied warranty of
 17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18    GNU General Public License for more details.
 19
 20    You should have received a copy of the GNU General Public License
 21    along with FLVMeta; if not, write to the Free Software
 22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 23*/
 24
 25#include "json.h"
 26#include <stdio.h>
 27#include <string.h>
 28
 29static void json_print_string(const char * str, size_t bytes) {
 30    size_t i;
 31    printf("\"");
 32    for (i = 0; i < bytes; ++i) {
 33        switch (str[i]) {
 34            case '\"': printf("\\\""); break;
 35            case '\\': printf("\\\\"); break;
 36            case '/':  printf("\\/");  break;
 37            case '\b': printf("\\b");  break;
 38            case '\f': printf("\\f");  break;
 39            case '\n': printf("\\n");  break;
 40            case '\r': printf("\\r");  break;
 41            case '\t': printf("\\t");  break;
 42            default: printf("%c", str[i]);
 43        }
 44    }
 45    printf("\"");
 46}
 47
 48static void json_print_comma(json_emitter * je) {
 49    if (je->print_comma != 0) {
 50        printf(",");
 51        je->print_comma = 0;
 52    }
 53}
 54
 55void json_emit_init(json_emitter * je) {
 56    je->print_comma = 0;
 57}
 58
 59void json_emit_object_start(json_emitter * je) {
 60    json_print_comma(je);
 61    printf("{");
 62}
 63
 64void json_emit_object_key(json_emitter * je, const char * str, size_t bytes) {
 65    json_print_comma(je);
 66    json_print_string(str, bytes);
 67    printf(":");
 68    je->print_comma = 0;
 69}
 70
 71void json_emit_object_key_z(json_emitter * je, const char * str) {
 72    json_print_comma(je);
 73    json_print_string(str, strlen(str));
 74    printf(":");
 75    je->print_comma = 0;
 76}
 77
 78void json_emit_object_end(json_emitter * je) {
 79    printf("}");
 80    je->print_comma = 1;
 81}
 82
 83void json_emit_array_start(json_emitter * je) {
 84    json_print_comma(je);
 85    printf("[");
 86}
 87
 88void json_emit_array_end(json_emitter * je) {
 89    printf("]");
 90    je->print_comma = 1;
 91}
 92
 93void json_emit_boolean(json_emitter * je, byte value) {
 94    json_print_comma(je);
 95    printf("%s", value != 0 ? "true" : "false");
 96    je->print_comma = 1;
 97}
 98
 99void json_emit_null(json_emitter * je) {
100    json_print_comma(je);
101    printf("null");
102    je->print_comma = 1;
103}
104
105void json_emit_integer(json_emitter * je, int value) {
106    json_print_comma(je);
107    printf("%i", value);
108    je->print_comma = 1;
109}
110
111void json_emit_file_offset(json_emitter * je, file_offset_t value) {
112    json_print_comma(je);
113    printf("%" FILE_OFFSET_PRINTF_FORMAT "i", value);
114    je->print_comma = 1;
115}
116
117void json_emit_number(json_emitter * je, number64 value) {
118    json_print_comma(je);
119    printf("%.12g", value);
120    je->print_comma = 1;
121}
122
123void json_emit_string(json_emitter * je, const char * str, size_t bytes) {
124    json_print_comma(je);
125    json_print_string(str, bytes);
126    je->print_comma = 1;
127}
128
129void json_emit_string_z(json_emitter * je, const char * str) {
130    json_print_comma(je);
131    json_print_string(str, strlen(str));
132    je->print_comma = 1;
133}