Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXJSON.h
1 /********************************************************************************
2 * *
3 * J S O N R e a d e r & W r i t e r *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2013,2021 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXJSON_H
22 #define FXJSON_H
23 
24 namespace FX {
25 
26 
64 class FXAPI FXJSON {
65 public:
66  enum Error {
67  ErrOK,
78  ErrEnd
79  };
80  enum Flow {
81  Stream,
83  Pretty
84  };
85  enum Direction {
86  Stop = 0,
87  Save = 1,
88  Load = 2
89  };
90 protected:
91  FXchar *begptr; // Text buffer begin ptr
92  FXchar *endptr; // Text buffer end ptr
93  FXchar *wptr; // Text buffer write ptr
94  FXchar *rptr; // Text buffer read ptr
95  FXchar *sptr; // Text buffer scan ptr
96  FXlong offset; // Position from start
97  FXint column; // Column number
98  FXint indent; // Indent level
99  FXint line; // Line number
100  Direction dir; // Direction
101  FXint token; // Token
102  FXint wrap; // Line wrap column
103  FXuchar flow; // Output flow
104  FXuchar prec; // Float precision
105  FXuchar fmt; // Float format
106  FXuchar esc; // Escape mode
107  FXuchar dent; // Indentation amount
108 private:
109  FXint next();
110  FXbool need(FXival count);
111  FXbool emit(const FXchar* str,FXival count);
112  FXbool emit(FXchar ch,FXival count);
113  Error loadString(FXString& str);
114  Error loadIdent(FXString& str);
115  Error loadMap(FXVariant& var);
116  Error loadArray(FXVariant& var);
117  Error loadVariant(FXVariant& var);
118  Error saveString(const FXString& str);
119  Error saveIdent(const FXString& str);
120  Error saveMap(const FXVariant& var);
121  Error saveArray(const FXVariant& var);
122  Error saveVariant(const FXVariant& var);
123 private:
124  static const FXchar *const errors[];
125 private:
126  FXJSON(const FXJSON&);
127  FXJSON &operator=(const FXJSON&);
128 public:
129 
133  FXJSON();
134 
139  FXJSON(FXchar* buffer,FXuval sz=8192,Direction d=Load);
140 
144  FXbool open(FXchar* buffer=NULL,FXuval sz=8192,Direction d=Load);
145 
149  Direction direction() const { return dir; }
150 
154  FXuval size() const { return endptr-begptr; }
155 
159  FXint getLine() const { return line; }
160 
164  FXint getColumn() const { return column; }
165 
169  FXlong getOffset() const { return offset; }
170 
175  Error load(FXVariant& variant);
176 
181  Error save(const FXVariant& variant);
182 
184  static const FXchar* getError(Error err){ return errors[err]; }
185 
191  void setNumericPrecision(FXint p){ prec=p; }
192  FXint getNumericPrecision() const { return prec; }
193 
204  void setNumericFormat(FXint f){ fmt=f; }
205  FXint getNumericFormat() const { return fmt; }
206 
216  void setOutputFlow(FXint f){ flow=f; }
217  FXint getOutputFlow() const { return flow; }
218 
223  void setIndentation(FXint d){ dent=d; }
224  FXint getIndentation() const { return dent; }
225 
229  void setLineWrap(FXint w){ wrap=w; }
230  FXint getLineWrap() const { return wrap; }
231 
242  void setEscapeMode(FXint e){ esc=e; }
243  FXint getEscapeMode() const { return esc; }
244 
248  virtual FXival fill(FXival count);
249 
253  virtual FXival flush(FXival count);
254 
258  FXbool close();
259 
263  virtual ~FXJSON();
264  };
265 
266 }
267 
268 #endif
void setEscapeMode(FXint e)
Change string escape mode.
Definition: FXJSON.h:242
A Variant type can hold any kind of object, be it a boolean, integer, real, string, or even array of Variants or dictionaries of variants.
Definition: FXVariant.h:44
No errors.
Definition: FXJSON.h:68
Flow
Definition: FXJSON.h:80
void setIndentation(FXint d)
Change indentation level for pretty print flow, the amount of indentation applied for each level...
Definition: FXJSON.h:223
Expected closing bracket.
Definition: FXJSON.h:74
Expected closing quotes.
Definition: FXJSON.h:76
Direction direction() const
Return direction in effect.
Definition: FXJSON.h:149
void setLineWrap(FXint w)
Change column at which lines are wrapped.
Definition: FXJSON.h:229
Illegal token.
Definition: FXJSON.h:71
FXint getColumn() const
Return current column number.
Definition: FXJSON.h:164
void setOutputFlow(FXint f)
Change output flow format (Stream, Compact, Pretty).
Definition: FXJSON.h:216
Direction
Definition: FXJSON.h:85
Unable to save.
Definition: FXJSON.h:69
The FXJSON serializer loads or saves an FXVariant to a JSON text file.
Definition: FXJSON.h:64
FXint getLine() const
Return current line number.
Definition: FXJSON.h:159
static const FXchar * getError(Error err)
Returns error code for given error.
Definition: FXJSON.h:184
Expected closing brace.
Definition: FXJSON.h:75
Definition: FX4Splitter.h:28
FXuval size() const
Return size of parse buffer.
Definition: FXJSON.h:154
Stream-of-consciousness output.
Definition: FXJSON.h:82
Unable to load.
Definition: FXJSON.h:70
Error
Definition: FXJSON.h:66
Expected colon &#39;:&#39;.
Definition: FXJSON.h:72
void setNumericFormat(FXint f)
Floating point output format control.
Definition: FXJSON.h:204
FXlong getOffset() const
Return offset from begin of file.
Definition: FXJSON.h:169
Expected comma &#39;,&#39;.
Definition: FXJSON.h:73
void setNumericPrecision(FXint p)
Floating point output precision control.
Definition: FXJSON.h:191
Expected closing quote.
Definition: FXJSON.h:77
FXString provides essential string manipulation capabilities in FOX.
Definition: FXString.h:42

Copyright © 1997-2020 Jeroen van der Zijp