rllib  1
Public Member Functions | List of all members
rlJSON Class Reference

#include <rljson.h>

Public Member Functions

 rlJSON ()
 
virtual ~rlJSON ()
 
int initRecord (rlJSONrecord *record)
 
int interpret (const char *line, rlJSONrecord *record)
 
int print (FILE *fout, rlJSONrecord *record, const char *indent="")
 

Detailed Description

class for a simple JSON

Definition at line 47 of file rljson.h.

Constructor & Destructor Documentation

◆ rlJSON()

rlJSON::rlJSON ( )
construct the class

Definition at line 18 of file rljson.cpp.

19 {
20 }

◆ ~rlJSON()

rlJSON::~rlJSON ( )
virtual
destruct the class

Definition at line 22 of file rljson.cpp.

23 {
24 }

Member Function Documentation

◆ initRecord()

int rlJSON::initRecord ( rlJSONrecord record)
init a rlJSONrecord

Definition at line 26 of file rljson.cpp.

27 {
28  record->startsWithText = 0; // "text"
29  record->startsWithOpenBracket = 0; // {
30  record->startsWithCloseBracket = 0; // }
31  record->startsWithEndArray = 0; // ]
32  record->text.setText(""); // white space in front of the tag name is ignored
33  record->optColon = 0; // a colon must follow when text is a tag
34  record->optOpenBracket = 0; // a openBracket { might follow
35  record->optBeginArray = 0; // a beginArray [ might follow
36  record->optParamType = 0; // 0=default=text 1=integer 2=float
37  record->param.setText(""); // the parameter in text form
38  record->intParam = 0; // 0 if there is no integer number
39  record->floatParam = 0.0f; // 0.0 if there is no floating point number
40  record->optComma = 0; // tells us that more elements follow
41  return 0;
42 }
float floatParam
Definition: rljson.h:40
rlString text
Definition: rljson.h:33
int startsWithText
Definition: rljson.h:29
int startsWithOpenBracket
Definition: rljson.h:30
int optOpenBracket
Definition: rljson.h:35
rlString param
Definition: rljson.h:38
int optComma
Definition: rljson.h:41
int intParam
Definition: rljson.h:39
int setText(const char *text)
Definition: rlstring.cpp:136
int optBeginArray
Definition: rljson.h:36
int startsWithEndArray
Definition: rljson.h:32
int startsWithCloseBracket
Definition: rljson.h:31
int optParamType
Definition: rljson.h:37
int optColon
Definition: rljson.h:34

◆ interpret()

int rlJSON::interpret ( const char *  line,
rlJSONrecord record 
)
interpret a JSON formatted line of text

Definition at line 44 of file rljson.cpp.

45 {
46  if(line == NULL) return -1;
47  if(record == NULL) return -1;
48  initRecord(record);
49 
50  int i=0;
51  while(line[i] == ' ') i++;
52  if (line[i] == '"') record->startsWithText = 1;
53  else if(line[i] == '{') record->startsWithOpenBracket = 1;
54  else if(line[i] == '}') record->startsWithCloseBracket = 1;
55  else if(line[i] == ']') record->startsWithEndArray = 1;
56 
57  if(record->startsWithText)
58  {
59  i++;
60  rlString text(&line[i]);
61  char *start = text.text();
62  char *end = strchr(start,'"');
63  if(end != NULL)
64  {
65  *end = '\0';
66  record->text = start;
67  end++;
68  start = end;
69  start = strchr(start,'"');
70  if(start != NULL)
71  {
72  start++;
73  end = strchr(start,'"');
74  if(end != NULL)
75  {
76  *end = '\0';
77  record->param = start;
78  }
79  }
80  }
81  }
82  else if(record->startsWithOpenBracket == 1)
83  {
84  }
85  else if(record->startsWithCloseBracket == 1)
86  {
87  }
88  else if(record->startsWithEndArray == 1)
89  {
90  }
91  else // this must be either an empty line or an integer or float parameter
92  {
93  if(strchr(line,'.') != NULL)
94  {
95  record->optParamType = 2; // float
96  record->param = &line[i];
97  char *cptr = record->param.text();
98  cptr = strchr(cptr,',');
99  if(cptr != NULL) *cptr = '\0';
100  }
101  else
102  {
103  if(line[i] >= '0' && line[i] <= '9') record->optParamType = 1; // integer
104  else if(line[i] == '-') record->optParamType = 1; // integer
105  else if(line[i] == '+') record->optParamType = 1; // integer
106  if(record->optParamType == 1) record->param = &line[i];
107  char *cptr = record->param.text();
108  cptr = strchr(cptr,',');
109  if(cptr != NULL) *cptr = '\0';
110  }
111  }
112 
113  if(strchr(line,':') != NULL) record->optColon = 1; // a colon must follow when text is a tag
114  if(strchr(line,'{') != NULL) record->optOpenBracket = 1; // a openBracket { might follow
115  if(strchr(line,'[') != NULL) record->optBeginArray = 1; // a beginArray [ might follow
116  if(strchr(line,',') != NULL) record->optComma = 1; // tells us that more elements follow
117 
118  return 0;
119 }
rlString text
Definition: rljson.h:33
int startsWithText
Definition: rljson.h:29
int startsWithOpenBracket
Definition: rljson.h:30
int optOpenBracket
Definition: rljson.h:35
char * text()
Definition: rlstring.cpp:126
rlString param
Definition: rljson.h:38
int optComma
Definition: rljson.h:41
int optBeginArray
Definition: rljson.h:36
int startsWithEndArray
Definition: rljson.h:32
int startsWithCloseBracket
Definition: rljson.h:31
int optParamType
Definition: rljson.h:37
int initRecord(rlJSONrecord *record)
Definition: rljson.cpp:26
int optColon
Definition: rljson.h:34

◆ print()

int rlJSON::print ( FILE *  fout,
rlJSONrecord record,
const char *  indent = "" 
)
print a JSON formatted line of text

Definition at line 121 of file rljson.cpp.

122 {
123  if(record == NULL) return -1;
124  if(indent != NULL) printf("%s",indent);
125  if(record->startsWithText)
126  {
127  fprintf(fout,"\"%s\"",record->text.text());
128  if(record->optOpenBracket == 0 && record->optBeginArray == 0)
129  {
130  if(record->optColon == 1)
131  {
132  fprintf(fout,": ");
133  if(record->optParamType == 0) fprintf(fout,"\"%s\"",record->param.text());
134  else fprintf(fout,"%s",record->param.text());
135  if(record->optComma == 1) fprintf(fout,",");
136  fprintf(fout,"\n");
137  }
138  if(record->optColon == 0)
139  {
140  if(record->optComma == 1) fprintf(fout,",");
141  fprintf(fout,"\n");
142  }
143  }
144  else if(record->optOpenBracket == 1)
145  {
146  if(record->optColon == 1) fprintf(fout,":");
147  fprintf(fout," ");
148  if(record->optParamType == 0) fprintf(fout,"\"%s\"",record->param.text());
149  else fprintf(fout,"%s",record->param.text());
150  fprintf(fout," {");
151  fprintf(fout,"\n");
152  }
153  else if(record->optBeginArray == 1)
154  {
155  if(record->optColon == 1) fprintf(fout,":");
156  fprintf(fout," [\n");
157  }
158  }
159  else if(record->startsWithOpenBracket == 1)
160  {
161  fprintf(fout,"{");
162  fprintf(fout,"\n");
163  }
164  else if(record->startsWithCloseBracket == 1)
165  {
166  fprintf(fout,"}");
167  if(record->optComma == 1) fprintf(fout,",");
168  fprintf(fout,"\n");
169  }
170  else if(record->startsWithEndArray == 1)
171  {
172  fprintf(fout,"]");
173  if(record->optComma == 1) fprintf(fout,",");
174  fprintf(fout,"\n");
175  }
176  return 0;
177 }
rlString text
Definition: rljson.h:33
int startsWithText
Definition: rljson.h:29
int startsWithOpenBracket
Definition: rljson.h:30
int optOpenBracket
Definition: rljson.h:35
char * text()
Definition: rlstring.cpp:126
rlString param
Definition: rljson.h:38
int optComma
Definition: rljson.h:41
int optBeginArray
Definition: rljson.h:36
int startsWithEndArray
Definition: rljson.h:32
int startsWithCloseBracket
Definition: rljson.h:31
int optParamType
Definition: rljson.h:37
int optColon
Definition: rljson.h:34

The documentation for this class was generated from the following files: