[sgen] Scan pinned objects in nursery as part of concurrent mark
[mono.git] / mono / utils / json.c
1 /*
2  * json.c: JSON writer
3  *
4  * Author:
5  *   Joao Matos (joao.matos@xamarin.com)
6  *
7  * Copyright 2015 Xamarin Inc (http://www.xamarin.com)
8  */
9
10 #include <mono/utils/json.h>
11
12 void mono_json_writer_init (JsonWriter* writer)
13 {
14         g_assert (writer && "Expected a valid JSON writer instance");
15
16         writer->text = g_string_new ("");
17         writer->indent = 0;
18 }
19
20 void mono_json_writer_destroy (JsonWriter* writer)
21 {
22         g_assert (writer && "Expected a valid JSON writer instance");
23         g_string_free (writer->text, /*free_segment=*/TRUE);
24 }
25
26 void mono_json_writer_indent_push(JsonWriter* writer)
27 {
28         g_assert (writer && "Expected a valid JSON writer instance");
29         writer->indent += JSON_INDENT_VALUE;
30 }
31
32 void mono_json_writer_indent_pop(JsonWriter* writer)
33 {
34         g_assert (writer && "Expected a valid JSON writer instance");
35         writer->indent -= JSON_INDENT_VALUE;
36 }
37
38 void mono_json_writer_indent(JsonWriter* writer)
39 {
40         g_assert (writer && "Expected a valid JSON writer instance");
41
42         int i = 0;
43         for (i = 0; i < writer->indent; ++i)
44                 g_string_append_c (writer->text, ' ');
45 }
46
47 void mono_json_writer_vprintf(JsonWriter* writer, const gchar *format, va_list args)
48 {
49         g_assert (writer && "Expected a valid JSON writer instance");
50         g_string_append_vprintf (writer->text, format, args);
51 }
52
53 void mono_json_writer_printf(JsonWriter* writer, const gchar *format, ...)
54 {
55         g_assert (writer && "Expected a valid JSON writer instance");
56
57         va_list args;
58         va_start (args, format);
59
60         g_string_append_vprintf (writer->text, format, args);
61
62         va_end (args);
63 }
64
65 void mono_json_writer_array_begin(JsonWriter* writer)
66 {
67         g_assert (writer && "Expected a valid JSON writer instance");
68         g_string_append_printf (writer->text, "[\n");
69         writer->indent += JSON_INDENT_VALUE;
70 }
71
72 void mono_json_writer_array_end(JsonWriter* writer)
73 {
74         g_assert (writer && "Expected a valid JSON writer instance");
75         g_string_append_printf (writer->text, "]");
76         writer->indent -= JSON_INDENT_VALUE;
77 }
78
79 void mono_json_writer_object_begin(JsonWriter* writer)
80 {
81         g_assert (writer && "Expected a valid JSON writer instance");
82         mono_json_writer_printf (writer, "{\n");
83         writer->indent += JSON_INDENT_VALUE;
84 }
85
86 void mono_json_writer_object_end(JsonWriter* writer)
87 {
88         g_assert (writer && "Expected a valid JSON writer instance");
89         mono_json_writer_printf (writer, "}");
90 }
91
92 void mono_json_writer_object_key(JsonWriter* writer, const gchar* format, ...)
93 {
94         g_assert (writer && "Expected a valid JSON writer instance");
95
96         va_list args;
97         va_start (args, format);
98
99         g_string_append_printf (writer->text, "\"");
100         mono_json_writer_vprintf (writer, format, args);
101         g_string_append_printf (writer->text, "\" : ");
102
103         va_end (args);
104 }