Merge pull request #2816 from xmcclure/profile-clean-0
[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  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9  */
10
11 #include <mono/utils/json.h>
12
13 void mono_json_writer_init (JsonWriter* writer)
14 {
15         g_assert (writer && "Expected a valid JSON writer instance");
16
17         writer->text = g_string_new ("");
18         writer->indent = 0;
19 }
20
21 void mono_json_writer_destroy (JsonWriter* writer)
22 {
23         g_assert (writer && "Expected a valid JSON writer instance");
24         g_string_free (writer->text, /*free_segment=*/TRUE);
25 }
26
27 void mono_json_writer_indent_push(JsonWriter* writer)
28 {
29         g_assert (writer && "Expected a valid JSON writer instance");
30         writer->indent += JSON_INDENT_VALUE;
31 }
32
33 void mono_json_writer_indent_pop(JsonWriter* writer)
34 {
35         g_assert (writer && "Expected a valid JSON writer instance");
36         writer->indent -= JSON_INDENT_VALUE;
37 }
38
39 void mono_json_writer_indent(JsonWriter* writer)
40 {
41         g_assert (writer && "Expected a valid JSON writer instance");
42
43         int i = 0;
44         for (i = 0; i < writer->indent; ++i)
45                 g_string_append_c (writer->text, ' ');
46 }
47
48 void mono_json_writer_vprintf(JsonWriter* writer, const gchar *format, va_list args)
49 {
50         g_assert (writer && "Expected a valid JSON writer instance");
51         g_string_append_vprintf (writer->text, format, args);
52 }
53
54 void mono_json_writer_printf(JsonWriter* writer, const gchar *format, ...)
55 {
56         g_assert (writer && "Expected a valid JSON writer instance");
57
58         va_list args;
59         va_start (args, format);
60
61         g_string_append_vprintf (writer->text, format, args);
62
63         va_end (args);
64 }
65
66 void mono_json_writer_array_begin(JsonWriter* writer)
67 {
68         g_assert (writer && "Expected a valid JSON writer instance");
69         g_string_append_printf (writer->text, "[\n");
70         writer->indent += JSON_INDENT_VALUE;
71 }
72
73 void mono_json_writer_array_end(JsonWriter* writer)
74 {
75         g_assert (writer && "Expected a valid JSON writer instance");
76         g_string_append_printf (writer->text, "]");
77         writer->indent -= JSON_INDENT_VALUE;
78 }
79
80 void mono_json_writer_object_begin(JsonWriter* writer)
81 {
82         g_assert (writer && "Expected a valid JSON writer instance");
83         mono_json_writer_printf (writer, "{\n");
84         writer->indent += JSON_INDENT_VALUE;
85 }
86
87 void mono_json_writer_object_end(JsonWriter* writer)
88 {
89         g_assert (writer && "Expected a valid JSON writer instance");
90         mono_json_writer_printf (writer, "}");
91 }
92
93 void mono_json_writer_object_key(JsonWriter* writer, const gchar* format, ...)
94 {
95         g_assert (writer && "Expected a valid JSON writer instance");
96
97         va_list args;
98         va_start (args, format);
99
100         g_string_append_printf (writer->text, "\"");
101         mono_json_writer_vprintf (writer, format, args);
102         g_string_append_printf (writer->text, "\" : ");
103
104         va_end (args);
105 }