summaryrefslogtreecommitdiff
path: root/libs/util/src/JSonWriter.cc
blob: ec9b1dc1254a9058e741c7af7470f54c50fd83da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
 * ======================== legal notice ======================
 * 
 * File:      JSonWriter.cc
 * Created:   6. Juli 2012, 12
 * Author:    <a href="mailto:geronimo013@gmx.de">Geronimo</a>
 * Project:   libutil - base classes used by other libraries
 * 
 * CMP - compound media player
 * 
 * is a client/server mediaplayer intended to play any media from any workstation
 * without the need to export or mount shares. cmps is an easy to use backend
 * with a (ready to use) HTML-interface. Additionally the backend supports
 * authentication via HTTP-digest authorization.
 * cmpc is a client with vdr-like osd-menues.
 * 
 * Copyright (c) 2012 Reinhard Mantey, some rights reserved!
 * published under Creative Commons by-sa
 * For details see http://creativecommons.org/licenses/by-sa/3.0/
 * 
 * The cmp project's homepage is at http://projects.vdr-developer.org/projects/cmp
 * 
 * --------------------------------------------------------------
 */
#include <JSonWriter.h>
#include <StringBuilder.h>
#include <Logging.h>
#include <stdio.h>
#define DEBUG 1

cJSonWriter::cJSonWriter(cStringBuilder &StringBuilder)
 : lastState(JS_Unknown)
 , sb(StringBuilder)
{
}

cJSonWriter::~cJSonWriter()
{
}

cJSonWriter::JSonState cJSonWriter::State()
{
  if (state.empty()) return JS_Unknown;
  return state.top();
}

void cJSonWriter::PushState(JSonState State)
{
  state.push(State);
}

cJSonWriter::JSonState cJSonWriter::PopState(void)
{
  lastState = State();
  state.pop();

  return lastState;
}

cJSonWriter &cJSonWriter::Object(void) {
  if (lastState == JS_Object) {
#ifdef DEBUG
    sb.Append(",\n");
#else
    sb.Append(", ");
#endif
  }
  sb.Append("{ ");
  PushState(JS_Object);
  lastState = JS_Unknown;
  return *this;
}

cJSonWriter &cJSonWriter::EndObject(void) {
  if (State() != JS_Object) {
     esyslog("JSonWriter::EndObject(): invalid status %d - should be %d", State(), JS_Object);
     }
  else {
     PopState();
     sb.Append(" }");
     }
  return *this;
}

cJSonWriter &cJSonWriter::Array(void) {
  if (State() != JS_Key) {
     esyslog("JSonWriter::Array(): invalid status %d - should be %d", State(), JS_Key);
     }
  else {
     sb.Append("[ ");
     PushState(JS_Array);
     lastState = JS_Unknown;
     }
  return *this;
}

cJSonWriter &cJSonWriter::EndArray(void) {
  if (State() != JS_Array) {
     esyslog("JSonWriter::EndArray(): invalid status %d - should be %d", State(), JS_Array);
     }
  else {
     sb.Append(" ]");
     PopState();
     if (State() == JS_Key) PopState(); // array is a value, so pop the key
     }
  return *this;
}

cJSonWriter &cJSonWriter::Key(const char *Name) {
  if (State() != JS_Object) {
     esyslog("JSonWriter::Key(): invalid status %d - should be %d", State(), JS_Object);
     }
  else {
     if (lastState == JS_Key) {
#ifdef DEBUG
       sb.Append(",\n");
#else
       sb.Append(", ");
#endif
     }
     sb.Append("\"").Append(Name).Append("\": ");
     PushState(JS_Key);
     }
  return *this;
}

cJSonWriter &cJSonWriter::Value(const char *Text) {
  if (State() != JS_Key) {
     esyslog("JSonWriter::Value(): invalid status %d - should be %d", State(), JS_Key);
     }
  else {
     PopState();
     sb.Append("\"").Append(Text).Append("\"");
     }
  return *this;
}

cJSonWriter &cJSonWriter::Value(int v) {
  if (State() != JS_Key) {
     esyslog("JSonWriter::Value(): invalid status %d - should be %d", State(), JS_Key);
     }
  else {
     PopState();
     sb.Append(v);
     }
  return *this;
}

cJSonWriter &cJSonWriter::Value(long v) {
  if (State() != JS_Key) {
     esyslog("JSonWriter::Value(): invalid status %d - should be %d", State(), JS_Key);
     }
  else {
     PopState();
     sb.Append(v);
     }
  return *this;
}

cJSonWriter &cJSonWriter::Value(size_t v) {
  if (State() != JS_Key) {
     esyslog("JSonWriter::Value(): invalid status %d - should be %d", State(), JS_Key);
     }
  else {
     PopState();
     sb.Append(v);
     }
  return *this;
}

cJSonWriter &cJSonWriter::Value(double v) {
  if (State() != JS_Key) {
     esyslog("JSonWriter::Value(): invalid status %d - should be %d", State(), JS_Key);
     }
  else {
     PopState();
     sb.Append(v);
     }
  return *this;
}

cJSonWriter &cJSonWriter::Value(bool v) {
  if (State() != JS_Key) {
     esyslog("JSonWriter::Value(): invalid status %d - should be %d", State(), JS_Key);
     }
  else {
     PopState();
     sb.Append(v, "true", "false");
     }
  return *this;
}