pando_json.c 706 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "pando_json.h"
  2. static char *json_buf;
  3. static int json_buf_len;
  4. static int pos;
  5. static int FUNCTION_ATTRIBUTE
  6. json_putchar(int c)
  7. {
  8. if (json_buf != NULL && pos < json_buf_len)
  9. {
  10. json_buf[pos++] = c;
  11. return c;
  12. }
  13. return 0;
  14. }
  15. int FUNCTION_ATTRIBUTE
  16. pando_json_print(struct jsontree_value * json_value, char * dst, int len)
  17. {
  18. if( dst == NULL)
  19. {
  20. return -1;
  21. }
  22. json_buf = dst;
  23. json_buf_len = len;
  24. pos = 0;
  25. struct jsontree_context js_ctx;
  26. js_ctx.values[0] = json_value;
  27. jsontree_reset(&js_ctx);
  28. js_ctx.putinchar = json_putchar;
  29. while (jsontree_print_next(&js_ctx));
  30. json_buf[pos] = 0;
  31. return pos;
  32. }