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