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
15BJsonWriter::BJsonWriter()
16	:
17	fErrorStatus(B_OK)
18{
19}
20
21
22BJsonWriter::~BJsonWriter()
23{
24}
25
26
27void
28BJsonWriter::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
41status_t
42BJsonWriter::ErrorStatus()
43{
44	return fErrorStatus;
45}
46
47
48status_t
49BJsonWriter::WriteBoolean(bool value)
50{
51	if (value)
52		return WriteTrue();
53
54	return WriteFalse();
55}
56
57
58status_t
59BJsonWriter::WriteTrue()
60{
61	Handle(BJsonEvent(B_JSON_TRUE));
62	return fErrorStatus;
63}
64
65
66status_t
67BJsonWriter::WriteFalse()
68{
69	Handle(BJsonEvent(B_JSON_FALSE));
70	return fErrorStatus;
71}
72
73
74status_t
75BJsonWriter::WriteNull()
76{
77	Handle(BJsonEvent(B_JSON_NULL));
78	return fErrorStatus;
79}
80
81
82status_t
83BJsonWriter::WriteInteger(int64 value)
84{
85	Handle(BJsonEvent(value));
86	return fErrorStatus;
87}
88
89
90status_t
91BJsonWriter::WriteDouble(double value)
92{
93	Handle(BJsonEvent(value));
94	return fErrorStatus;
95}
96
97
98status_t
99BJsonWriter::WriteString(const char* value)
100{
101	Handle(BJsonEvent(value));
102	return fErrorStatus;
103}
104
105
106status_t
107BJsonWriter::WriteObjectStart()
108{
109	Handle(BJsonEvent(B_JSON_OBJECT_START));
110	return fErrorStatus;
111}
112
113
114status_t
115BJsonWriter::WriteObjectName(const char* value)
116{
117	Handle(BJsonEvent(B_JSON_OBJECT_NAME, value));
118	return fErrorStatus;
119}
120
121
122status_t
123BJsonWriter::WriteObjectEnd()
124{
125	Handle(BJsonEvent(B_JSON_OBJECT_END));
126	return fErrorStatus;
127}
128
129
130status_t
131BJsonWriter::WriteArrayStart()
132{
133	Handle(BJsonEvent(B_JSON_ARRAY_START));
134	return fErrorStatus;
135}
136
137
138status_t
139BJsonWriter::WriteArrayEnd()
140{
141	Handle(BJsonEvent(B_JSON_ARRAY_END));
142	return fErrorStatus;
143}
144
145