/src/json.c

https://code.google.com/ · C · 133 lines · 93 code · 17 blank · 23 comment · 5 complexity · 4fde312ea8ebe6e5cb83f1913d8bbf1f MD5 · raw file

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