1 /*
2 * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>
3 * Distributed under the terms of the MIT License.
4 */
5
6
7 #include "JsonWriter.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include <UnicodeChar.h>
13
14
BJsonWriter()15 BJsonWriter::BJsonWriter()
16 :
17 fErrorStatus(B_OK)
18 {
19 }
20
21
~BJsonWriter()22 BJsonWriter::~BJsonWriter()
23 {
24 }
25
26
27 void
HandleError(status_t status,int32 line,const char * message)28 BJsonWriter::HandleError(status_t status, int32 line,
29 const char* message)
30 {
31 if(fErrorStatus == B_OK) {
32 if (message == NULL)
33 message = "?";
34 fErrorStatus = status;
35 fprintf(stderr, "! json err @line %" B_PRIi32 " - %s : %s\n", line,
36 strerror(status), message);
37 }
38 }
39
40
41 status_t
ErrorStatus()42 BJsonWriter::ErrorStatus()
43 {
44 return fErrorStatus;
45 }
46
47
48 status_t
WriteBoolean(bool value)49 BJsonWriter::WriteBoolean(bool value)
50 {
51 if (value)
52 return WriteTrue();
53
54 return WriteFalse();
55 }
56
57
58 status_t
WriteTrue()59 BJsonWriter::WriteTrue()
60 {
61 Handle(BJsonEvent(B_JSON_TRUE));
62 return fErrorStatus;
63 }
64
65
66 status_t
WriteFalse()67 BJsonWriter::WriteFalse()
68 {
69 Handle(BJsonEvent(B_JSON_FALSE));
70 return fErrorStatus;
71 }
72
73
74 status_t
WriteNull()75 BJsonWriter::WriteNull()
76 {
77 Handle(BJsonEvent(B_JSON_NULL));
78 return fErrorStatus;
79 }
80
81
82 status_t
WriteInteger(int64 value)83 BJsonWriter::WriteInteger(int64 value)
84 {
85 Handle(BJsonEvent(value));
86 return fErrorStatus;
87 }
88
89
90 status_t
WriteDouble(double value)91 BJsonWriter::WriteDouble(double value)
92 {
93 Handle(BJsonEvent(value));
94 return fErrorStatus;
95 }
96
97
98 status_t
WriteString(const char * value)99 BJsonWriter::WriteString(const char* value)
100 {
101 Handle(BJsonEvent(value));
102 return fErrorStatus;
103 }
104
105
106 status_t
WriteObjectStart()107 BJsonWriter::WriteObjectStart()
108 {
109 Handle(BJsonEvent(B_JSON_OBJECT_START));
110 return fErrorStatus;
111 }
112
113
114 status_t
WriteObjectName(const char * value)115 BJsonWriter::WriteObjectName(const char* value)
116 {
117 Handle(BJsonEvent(B_JSON_OBJECT_NAME, value));
118 return fErrorStatus;
119 }
120
121
122 status_t
WriteObjectEnd()123 BJsonWriter::WriteObjectEnd()
124 {
125 Handle(BJsonEvent(B_JSON_OBJECT_END));
126 return fErrorStatus;
127 }
128
129
130 status_t
WriteArrayStart()131 BJsonWriter::WriteArrayStart()
132 {
133 Handle(BJsonEvent(B_JSON_ARRAY_START));
134 return fErrorStatus;
135 }
136
137
138 status_t
WriteArrayEnd()139 BJsonWriter::WriteArrayEnd()
140 {
141 Handle(BJsonEvent(B_JSON_ARRAY_END));
142 return fErrorStatus;
143 }
144
145