1
// ---------------------------------------------------------------------------
3
// - afnix:wax service - json mime class implementation -
4
// ---------------------------------------------------------------------------
5
// - This program is free software; you can redistribute it and/or modify -
6
// - it provided that this copyright notice is kept intact. -
8
// - This program is distributed in the hope that it will be useful, but -
9
// - without any warranty; without even the implied warranty of -
10
// - merchantability or fitness for a particular purpose. In no event shall -
11
// - the copyright holder be liable for any direct, indirect, incidental or -
12
// - special damages arising in any way out of the use of this software. -
13
// ---------------------------------------------------------------------------
14
// - copyright (c) 1999-2015 amaury darsch -
15
// ---------------------------------------------------------------------------
18
#include "Utility.hpp"
19
#include "Rsamples.hpp"
20
#include "JsonMime.hpp"
21
#include "Runnable.hpp"
22
#include "QuarkZone.hpp"
23
#include "Exception.hpp"
24
#include "OutputFile.hpp"
28
// -------------------------------------------------------------------------
29
// - private section -
30
// -------------------------------------------------------------------------
32
// the json content type
33
static const String JSWR_MIME_TYPE = "application/json";
35
// the rsamples data array constants
36
static const String JSWR_RSPL_MODE = "\"mode\"";
37
static const String JSWR_RSPL_HIDX = "\"hidx\"";
38
static const String JSWR_RSPL_TIME = "\"time\"";
39
static const String JSWR_RSPL_DATA = "\"data\"";
40
static const String JSWR_RSPL_NAME = "\"RSAMPLES\"";
42
// this procedure writes a rsamples object to a buffer
43
static void json_write (const Rsamples& data, Buffer& buf) {
45
String name = data.getname ();
46
// collect data elements
47
long rows = data.getrows ();
48
long cols = data.getcols ();
49
long rmax = rows - 1L;
50
long cmax = cols - 1L;
51
bool sflg = data.getsflg ();
52
long psiz = data.getpsiz ();
55
// write the name first if any
56
line = "{"; line+= eolc;
57
if (name.isnil () == false) {
58
line = name; line+= " = {"; line+= eolc;
62
line = " "; line+= JSWR_RSPL_MODE; line += " : "; line+= JSWR_RSPL_NAME;
63
line+= ','; line+= eolc;
66
line = " "; line+= JSWR_RSPL_HIDX; line += " : "; line+= "null";
67
line+= ','; line+= eolc;
70
line = " "; line+= JSWR_RSPL_TIME; line+= " : ";
72
if (data.stamped () == true) {
73
line = "["; line += eolc;
75
for (long k = 0; k < rows; k++) {
76
t_real t = data.gettime (k);
77
String s = Utility::tostring (t, psiz, sflg);
79
if (k < rmax) line+= ",";
83
line = " ],"; line+= eolc;
86
line = "null,"; line+= eolc;
90
line = " \"data\" : ["; line+= eolc;
92
for (long j = 0; j < cols; j++) {
93
line = " ["; line+= eolc;
95
for (long k = 0; k < rows; k++) {
96
t_real v = data.get (k, j);
97
String s = Utility::tostring (v, psiz, sflg);
98
line = " "; line += s;
99
if (k < rmax) line+= ",";
110
line = " ]"; line+= eolc;
113
line = "}"; line+= eolc;
117
// this procedure writes a rsamples object to an output stream
118
static void json_write (const Rsamples& data, OutputStream& os) {
119
// collect name and info
120
String name = data.getname ();
121
String info = data.getinfo ();
122
// check for valid name and stram
123
if (name.isnil () == true) {
124
throw Exception ("json-error", "invalid nil name for mime writer");
126
// collect data elements
127
long rows = data.getrows ();
128
long cols = data.getcols ();
129
long rmax = rows - 1L;
130
long cmax = cols - 1L;
131
bool sflg = data.getsflg ();
132
long psiz = data.getpsiz ();
135
// write the name first if any
137
if (name.isnil () == false) {
138
line = name; line+= " = {";
142
line = " "; line +=JSWR_RSPL_MODE; line += " : "; line+= JSWR_RSPL_NAME;
146
line = " "; line +=JSWR_RSPL_HIDX; line += " : "; line+= "null";
150
line = " "; line+= JSWR_RSPL_TIME; line+= " : ";
152
if (data.stamped () == true) {
154
for (long k = 0; k < rows; k++) {
155
t_real t = data.gettime (k);
156
String s = Utility::tostring (t, psiz, sflg);
157
line = " "; line+= s;
158
if (k < rmax) line+= ",";
163
os.writeln ("null,");
166
os.writeln (" \"data\" : [");
167
for (long j = 0; j < cols; j++) {
169
for (long k = 0; k < rows; k++) {
170
t_real v = data.get (k, j);
171
String s = Utility::tostring (v, psiz, sflg);
172
line = " "; line += s;
173
if (k < rmax) line+= ",";
186
// -------------------------------------------------------------------------
188
// -------------------------------------------------------------------------
190
// create a default json mime
192
JsonMime::JsonMime (void) {
193
d_mime = JSWR_MIME_TYPE;
197
// create a json mime by object
199
JsonMime::JsonMime (Object* mobj) {
200
d_mime = JSWR_MIME_TYPE;
201
Object::iref (p_mobj = mobj);
204
// destroy this writer
206
JsonMime::~JsonMime (void) {
207
Object::dref (p_mobj);
210
// return the object class name
212
String JsonMime::repr (void) const {
216
// write the data samples to a buffer
218
void JsonMime::write (Buffer& buf) const {
221
// check for rsamples object
222
Rsamples* data = dynamic_cast <Rsamples*> (p_mobj);
223
if (data != nilp) json_write (*data, buf);
231
// write the data samples to an output stream
233
void JsonMime::write (OutputStream& os) const {
236
// check for rsamples object
237
Rsamples* data = dynamic_cast <Rsamples*> (p_mobj);
238
if (data != nilp) json_write (*data, os);
246
// get the mime object
248
Object* JsonMime::getobj (void) const {
251
Object* result = p_mobj;
260
// -------------------------------------------------------------------------
261
// - object section -
262
// -------------------------------------------------------------------------
265
static const long QUARK_ZONE_LENGTH = 1;
266
static QuarkZone zone (QUARK_ZONE_LENGTH);
268
// the object supported quarks
269
static const long QUARK_GETOBJ = zone.intern ("get-object");
271
// create a new object in a generic way
273
Object* JsonMime::mknew (Vector* argv) {
274
long argc = (argv == nilp) ? 0 : argv->length ();
275
// create a default html tree object
276
if (argc == 0) return new JsonMime;
277
// check for 1 argument
279
Object* obj = argv->get (0);
280
return new JsonMime (obj);
282
throw Exception ("argument-error",
283
"too many argument with json mime constructor");
286
// return true if the given quark is defined
288
bool JsonMime::isquark (const long quark, const bool hflg) const {
291
if (zone.exists (quark) == true) {
295
bool result = hflg ? Mime::isquark (quark, hflg) : false;
304
// apply this object with a set of arguments and a quark
306
Object* JsonMime::apply (Runnable* robj, Nameset* nset, const long quark,
308
// get the number of arguments
309
long argc = (argv == nilp) ? 0 : argv->length ();
311
// dispatch 0 argument
313
if (quark == QUARK_GETOBJ) {
316
Object* result = getobj ();
326
// call the mime method
327
return Mime::apply (robj, nset, quark, argv);