5 * Joao Matos (joao.matos@xamarin.com)
7 * Copyright 2015 Xamarin Inc (http://www.xamarin.com)
8 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
11 #include <mono/utils/json.h>
13 void mono_json_writer_init (JsonWriter
* writer
)
15 g_assert (writer
&& "Expected a valid JSON writer instance");
17 writer
->text
= g_string_new ("");
21 void mono_json_writer_destroy (JsonWriter
* writer
)
23 g_assert (writer
&& "Expected a valid JSON writer instance");
24 g_string_free (writer
->text
, /*free_segment=*/TRUE
);
27 void mono_json_writer_indent_push(JsonWriter
* writer
)
29 g_assert (writer
&& "Expected a valid JSON writer instance");
30 writer
->indent
+= JSON_INDENT_VALUE
;
33 void mono_json_writer_indent_pop(JsonWriter
* writer
)
35 g_assert (writer
&& "Expected a valid JSON writer instance");
36 writer
->indent
-= JSON_INDENT_VALUE
;
39 void mono_json_writer_indent(JsonWriter
* writer
)
41 g_assert (writer
&& "Expected a valid JSON writer instance");
44 for (i
= 0; i
< writer
->indent
; ++i
)
45 g_string_append_c (writer
->text
, ' ');
48 void mono_json_writer_vprintf(JsonWriter
* writer
, const gchar
*format
, va_list args
)
50 g_assert (writer
&& "Expected a valid JSON writer instance");
51 g_string_append_vprintf (writer
->text
, format
, args
);
54 void mono_json_writer_printf(JsonWriter
* writer
, const gchar
*format
, ...)
56 g_assert (writer
&& "Expected a valid JSON writer instance");
59 va_start (args
, format
);
61 g_string_append_vprintf (writer
->text
, format
, args
);
66 void mono_json_writer_array_begin(JsonWriter
* writer
)
68 g_assert (writer
&& "Expected a valid JSON writer instance");
69 g_string_append_printf (writer
->text
, "[\n");
70 writer
->indent
+= JSON_INDENT_VALUE
;
73 void mono_json_writer_array_end(JsonWriter
* writer
)
75 g_assert (writer
&& "Expected a valid JSON writer instance");
76 g_string_append_printf (writer
->text
, "]");
77 writer
->indent
-= JSON_INDENT_VALUE
;
80 void mono_json_writer_object_begin(JsonWriter
* writer
)
82 g_assert (writer
&& "Expected a valid JSON writer instance");
83 mono_json_writer_printf (writer
, "{\n");
84 writer
->indent
+= JSON_INDENT_VALUE
;
87 void mono_json_writer_object_end(JsonWriter
* writer
)
89 g_assert (writer
&& "Expected a valid JSON writer instance");
90 mono_json_writer_printf (writer
, "}");
93 void mono_json_writer_object_key(JsonWriter
* writer
, const gchar
* format
, ...)
95 g_assert (writer
&& "Expected a valid JSON writer instance");
98 va_start (args
, format
);
100 g_string_append_printf (writer
->text
, "\"");
101 mono_json_writer_vprintf (writer
, format
, args
);
102 g_string_append_printf (writer
->text
, "\" : ");