52#include <webclient/web_buffers.h>
116 int m_nCurrent_Position;
118 JsonRef() {parser = 0; m_pCurrent_Pool = 0; m_nCurrent_Position = 0;}
121 int printhelper(
CharOutputFn *pf,
void *p,
bool pretty);
144 const char *CurrentName()
const;
145 bool CurrentNameMatches(
const char *name)
const;
146 const char *CurrentString()
const;
147 int CopyCurrentString(
char *buf,
int buflen)
const;
148 int CopyCurrentString(
NBString &nbs)
const;
149 double CurrentNumber()
const;
150 inline double CurrentIntNumber()
const {
double dv=CurrentNumber();
if(isnan(dv))
return 0;
return dv;};
151 bool CurrentBool()
const;
152 bool PermissiveCurrentBool()
const;
232 if(t==pt1)
return prf;
233 if((t==pt2) &&(t!=
UNDEFINED))
return prf;
250inline JsonRef Number(
const char * name) {
return GetA(name,
NUMBER); };
300 operator bool ()
const {
return PermissiveCurrentBool(); };
305 operator float ()
const {
return (
float)CurrentNumber(); };
310 operator double ()
const {
return CurrentNumber(); };
315 operator uint8_t()
const {
return (uint8_t)CurrentIntNumber(); };
320 operator int()
const {
return (
int)CurrentIntNumber(); };
325 operator uint16_t()
const {
return (uint16_t)CurrentIntNumber(); };
330 operator uint32_t()
const {
return (uint32_t)CurrentIntNumber(); };
335 operator int8_t()
const {
return (int8_t)CurrentIntNumber(); };
340 operator int16_t()
const {
return (int16_t)CurrentIntNumber(); };
345 operator int32_t()
const {
return (uint32_t)CurrentIntNumber(); };
350 operator time_t()
const {
return (time_t)CurrentIntNumber(); };
355 operator const char *()
const {
return CurrentString(); };
402 inline bool Valid()
const {
return ((m_pCurrent_Pool) && (parser));};
525inline bool Valid(
const JsonRef & ref) {
return ref.
Valid(); }
548 bool m_syntaxValid =
true;
551 unsigned char m_BoolFlags;
552 const unsigned char fEnableLargeStrings=1;
553 const unsigned char fNeedComma=2;
554 const unsigned char fFirstFind=4;
555 const unsigned char fNeedNameTerm=8;
557 inline bool EnableLargeStrings() {
return ((m_BoolFlags & fEnableLargeStrings)!=0);};
558 inline bool NeedComma() {
return ((m_BoolFlags & fNeedComma)!=0);};
559 inline bool NeedNameTerm() {
return ((m_BoolFlags & fNeedNameTerm)!=0);};
560 inline void CheckLastComma() {
if(m_LastEmit==
VALUE_SEPERATOR){Error(
"Trailing comma");}};
562 inline void pushStack(
char c) {m_stateStack+=c; };
564 inline bool popStack(
char c)
565 {
int len=m_stateStack.
length();
566 if((len==0) || (m_stateStack[len-1]!=c))
572 inline char topStack()
573 {
int len=m_stateStack.
length();
575 return m_stateStack[len-1];
583 if(prev==
NAME)
return true;
603 unsigned char *m_pPrevStringStart;
606 unsigned char *PutChar(
unsigned char c);
607 void CheckForAndFixBrokenString();
608 void ProcessChar(
unsigned char c);
609 void ZeroNextString();
610 void ZeroNextNumber(
char c);
611 void AddStringChar(
char c);
612 void AddEscapedStringChar(
char c);
613 void AddNumberChar(
char c);
614 void EmitNumberBuffer();
618 inline bool IncPosition() {
return m_ref.
IncPosition(); };
619 inline bool JumpPosition(
int siz) {
return m_ref.
JumpPosition(siz); };
622 virtual void Error(
const char *err);
624 virtual void Warn(
const char *err);
647 inline void DiagDump(
const char * lab) {m_ref.
DiagDump(lab);};
661 if(b) m_BoolFlags|=fEnableLargeStrings;
663 m_BoolFlags&=~fEnableLargeStrings;
667 inline void NeedComma(
bool b)
669 if(b) m_BoolFlags|=fNeedComma;
671 m_BoolFlags&=~fNeedComma;
675 inline bool FirstFind() {
return ((m_BoolFlags & fFirstFind)!=0);};
676 inline void FirstFind(
bool b)
678 if(b) m_BoolFlags|=fFirstFind;
680 m_BoolFlags&=~fFirstFind;
696 virtual int WriteData(
const unsigned char *pCopyFrom,
int numBytes);
1119 {
return m_ref.FindElementAfterNameInCurrentArray(name);};
1159 inline int CopyCurrentString(
char *buf,
int buflen) {
return m_ref.CopyCurrentString(buf, buflen); };
1175 inline bool CurrentNameMatches(
const char *name) {
return m_ref.CurrentNameMatches(name); };
1471 return nan(
"quiet");
1485 return nan(
"quiet");
1498 return nan(
"quiet");
1511 return nan(
"quiet");
1905#include <fd_adapter.h>
1908 virtual int read(
char *buf,
int nbytes);
1909 virtual int write(
const char *buf,
int nbytes);
1910 virtual int close();
Used to store and manipulate IPv4 addresses in dual stack mode.
Definition nettypes.h:225
Used to hold and manipulate IPv4 and IPv6 addresses in dual stack mode.
Definition ipv6_addr.h:41
Get the type name of the JSON primitive type passed in.
Definition json_lexer.h:1907
Represents a positional reference (pointer) of a location inside a ParsedJsonDataSet object.
Definition json_lexer.h:113
int GetChildPrintSize(bool pretty=false)
Return the length of the child objects' JSON text as written with the PrintChildren* functions.
JsonRef operator()(const char *namev)
Get a JsonRef representing any entity from a parent object by key name.
Definition json_lexer.h:282
bool IsNull()
Check if the current JsonRef is a valid JSON null element.
Definition json_lexer.h:392
void DiagDump(const char *lab)
Output a verbose diagnostic report of the ParsedJsonDatSet to stdout.
int PrintChildren(bool pretty=false)
Output child objects of this JsonRef as JSON text to stdout.
JsonRef operator[](int i)
Get a JsonRef from an array by numerical index.
bool IsString()
Check if the current JsonRef is a valid JSON string element.
Definition json_lexer.h:382
bool IsObject()
Check if the current JsonRef is a valid JSON object element.
Definition json_lexer.h:376
JsonRef start()
Begin traversal: check for ref validity and reset the position.
Definition json_lexer.h:415
int PrintChildrenToFd(int fd, bool pretty=false)
Output child objects of this JsonRef as JSON text to a file descriptor.
bool IsArray()
Check if the current JsonRef is a valid JSON array element.
Definition json_lexer.h:397
void SkipArray()
Increment the position index of the ParsedJsonDataSet beyond this array.
JsonRef next() const
Traverse: check for ref validity and increment the position.
Definition json_lexer.h:427
bool IsBool()
Check if the current JsonRef is a valid JSON boolean element.
Definition json_lexer.h:387
int PrintChildrenToBuffer(char *buffer, int maxlen, bool pretty=false)
Output child objects of this JsonRef as JSON text to a buffer pointer.
bool Valid() const
Check if the current JsonRef is a valid JSON position.
Definition json_lexer.h:402
void SkipElement()
Increment the position index of the ParsedJsonDataSet beyond this element.
void ResetPosition()
Reset the position index of the ParsedJsonDataSet to the beginning.
int PrintChildrenToString(NBString &s, bool pretty=false)
Output child objects of this JsonRef as JSON text to a NBString.
bool IsNumber()
Check if the current JsonRef is a valid JSON number element.
Definition json_lexer.h:371
bool JumpPosition(int siz)
Jump to a specific position index in the ParsedJsonDataSet.
bool IncPosition()
Increment the position index of the ParsedJsonDataSet.
JsonRef name(const char *name)
Get a JsonRef representing any entity from a parent object by key name.
void SkipObject()
Increment the position index of the ParsedJsonDataSet beyond this object.
JsonRef object(const char *name)
Get a JsonRef representing an object from a parent object by key name.
Definition json_lexer.h:188
void MakeInvalid()
Definition json_lexer.h:518
Used to store and manipulate MAC addresses.
Definition nettypes.h:69
Lightweight alternative to C++ CString class.
Definition nbstring.h:118
size_t length() const
Returns the length of the string.
void shrink(int n)
Reduces the length of the string by n.
A class to create, read, and modify a JSON object.
Definition json_lexer.h:535
ParsedJsonDataSet * AddMyMac(const char *name)
Add the device MAC address to the JSON data set.
ParsedJsonDataSet * Add(const char *name, unsigned long i)
Add a name/value pair to the JSON data set where the value is a unsigned long.
ParsedJsonDataSet * Add(const char *name, unsigned short i)
Add a name/value pair to the JSON data set where the value is a unsigned short.
bool FindGlobalPermissiveBoolean(const char *name)
Find the permissive boolean value of a given element. Starts at the current position and then starts ...
Definition json_lexer.h:1257
json_primitive_type GetNextArray()
Iterates the current element of the JSON data set until it finds one of type BEGIN_ARRAY.
json_primitive_type FindElementAfterNameInCurrentArray(const char *name)
Looks for elements with the current name in the current ARRAY only. Does not search sub arrays.
Definition json_lexer.h:1118
const char * CurrentName()
Get the name of the current element.
Definition json_lexer.h:1174
json_primitive_type GetNextNumberInCurrentArray()
Get the next element of type NUMBER that exists in the current ARRAY after the current position.
ParsedJsonDataSet * Add(const char *name, const MACADR &ma)
Add a name/value pair to the JSON data set where the value is a MAC address.
json_primitive_type FindGlobalElementAfterName(const char *name)
Finds name in current object points at element after name. This only supports simple,...
Definition json_lexer.h:1099
int PrintChildrenToFd(int fd, bool pretty=false)
Output child objects of this JsonRef as JSON text to a file descriptor.
Definition json_lexer.h:1634
int FindAndCopyString(const char *name, char *buf, int buflen)
Find and copy the string value of the element with the given name If not provided a buffer or the pro...
Definition json_lexer.h:1421
ParsedJsonDataSet * AddArrayElement(const char *str)
Add a string to the current array.
double FindGlobalNumber(const char *name)
Find the number value of a given element. Starts at the current position and then starts again at the...
Definition json_lexer.h:1482
JsonRef SetParsePosition(JsonRef pos)
Sets the current parse position object for the JSON data set.
int PrintObjectToBuffer(char *buffer, int maxlen, bool pretty=false)
Prints the JSON data set to a provided buffer.
Definition json_lexer.h:1594
json_primitive_type GetNextObjectInCurrentArray()
Get the next element of type BEGIN_OBJECT that exists in the current ARRAY after the current position...
int PrintChildrenToBuffer(char *buffer, int maxlen, bool pretty=false)
Output child objects of this JsonRef as JSON text to a buffer pointer.
Definition json_lexer.h:1640
ParsedJsonDataSet * Add(const char *name, unsigned int i)
Add a name/value pair to the JSON data set where the value is an unsigned int.
JsonRef object(const char *name)
Get a JsonRef representing an object from a parent object by key name.
Definition json_lexer.h:779
double FindNumberInCurentObject(const char *name)
Find the number value of the element with the given name in the current object. Does not search sub-o...
Definition json_lexer.h:1508
json_primitive_type FindFullName(const char *name)
Find the element in the data set with the given name and move the parser to the next element....
Definition json_lexer.h:1067
json_primitive_type FindElementAfterNameInCurrentObject(const char *name)
Looks for elements with the current name in the current OBJECT only. Does not search sub objects.
Definition json_lexer.h:1109
bool FindGlobalObject(const char *name)
Find the OBJECT with the given name. Starts at the current position and then starts again at the begi...
Definition json_lexer.h:1524
json_primitive_type GetNextBoolInCurrentArray()
Get the next element of type TRUE_EL that exists in the current ARRAY after the current position.
json_primitive_type FindFullAtName(const char *name)
Find the element in the data set with the given name and move the parser to that element....
Definition json_lexer.h:1078
json_primitive_type GetNextObject()
Get the next element of type name that exists in the current OBJECT after the current position.
ParsedJsonDataSet * Add(const char *name, short i)
Add a name/value pair to the JSON data set where the value is a short.
json_primitive_type GetFirst()
Get the first element of the JSON data set.
Definition json_lexer.h:813
bool FindFullNamePermissiveBoolean(const char *name)
Find the permissive boolean value of a given element.
Definition json_lexer.h:1242
JsonRef name(const char *name)
Get a JsonRef representing any entity from a parent object by key name.
Definition json_lexer.h:768
bool FindGlobalBoolean(const char *name)
Find the boolean value of a given element. Starts at the current position and then starts again at th...
Definition json_lexer.h:1200
ParsedJsonDataSet * AddArrayElement(unsigned int i)
Add an unsigned int value to the current array.
ParsedJsonDataSet * EndObject()
Add an end to the current object.
ParsedJsonDataSet * AddNullArrayElement()
Add a null element to the current array.
ParsedJsonDataSet * Add(const char *name, const char *str)
Add a name/value pair to the JSON data set where the value is a string.
int FindAndCopyFullNameString(const char *name, NBString &nbs)
Find and copy the string value of the element with the given name in the current object....
Definition json_lexer.h:1334
bool FindFullNameBoolean(const char *name)
Find the boolean value of a given element.
Definition json_lexer.h:1185
ParsedJsonDataSet * AddArrayElement(unsigned short i)
Add an unsigned short value to the current array.
bool FindBooleanInCurentObject(const char *name)
Find the boolean value of the element within the current object. Does not search sub-objects.
Definition json_lexer.h:1228
const char * CurrentString()
Get the string value of the current element.
Definition json_lexer.h:1151
bool FindPermissiveBoolean(const char *name)
Find the permissive boolean value of the element after the element with the provided name.
Definition json_lexer.h:1271
virtual int WriteData(const unsigned char *pCopyFrom, int numBytes)
Writes the passed in data to the JSON data set.
void ResetPosition()
Resets the parser position to the beginning of the JSON data set.
Definition json_lexer.h:986
json_primitive_type GetNextStringInCurrentArray()
Get the next element of type STRING that exists in the current ARRAY after the current position.
ParsedJsonDataSet * Add(const char *name, double d)
Add a name/value pair to the JSON data set where the value is a double.
json_primitive_type GetNextNameInCurrentObject()
Get the next element of type NAME that exists in the current OBJECT after the current position.
Definition json_lexer.h:906
json_primitive_type GetCurrent()
Get the element at the current position of the JSON data set.
Definition json_lexer.h:843
ParsedJsonDataSet * AddNull(const char *name)
Add a name/value pair to the JSON data set where the value is null.
json_primitive_type SkipCurrentValue()
Skips over the current value, and get the next element. If called inside an ARRAY or OBJECT,...
Definition json_lexer.h:974
json_primitive_type GetRawCurrent()
Get the element at the current position of the JSON data set, including non-public types.
Definition json_lexer.h:859
bool FindPermissiveBooleanInCurentObject(const char *name)
Find the permissive boolean value of the element with the given name in the current object....
Definition json_lexer.h:1285
ParsedJsonDataSet * AddArrayElement(bool b)
Add a bool to the current array.
ParsedJsonDataSet * AddArrayStart(const char *name)
Add an ARRAY start to the JSON data set.
bool PermissiveCurrentBool()
Returns true if the current element a TRUE_EL primitive type, is "True", "true", or is a non-zero num...
Definition json_lexer.h:1135
int FindAndCopyString(const char *name, NBString &nbs)
Find and copy the string value of the element with the given name If not provided a buffer or the pro...
Definition json_lexer.h:1439
ParsedJsonDataSet * Add(const char *name, IPADDR4 i4)
Add a name/value pair to the JSON data set where the value is an IPv4 address.
ParsedJsonDataSet * Add(const char *name, int i)
Add a name/value pair to the JSON data set where the value is an int.
JsonRef GetParsePosition()
Gets the current parse position object for the JSON data set.
virtual int ReadFrom(int fd)
Reads in data from the specified file descriptor and parses it into the JSON data set.
ParsedJsonDataSet * AddArrayObjectStart()
Add the start of an object element to the current array.
bool FindBoolean(const char *name)
Find the boolean value of the element after the element with the given name.
Definition json_lexer.h:1214
json_primitive_type FindElementAfterName(const char *name)
Finds name in current object points at element after name. This only supports simple,...
Definition json_lexer.h:1088
int GetPrintSize(bool pretty=false)
Calculates how many characters the JSON data set would take to print.
Definition json_lexer.h:1649
int PrintChildren(bool pretty=false)
Output child objects of this JsonRef as JSON text to stdout.
Definition json_lexer.h:1628
int PrintObjectToFd(int fd, bool pretty=false)
Prints the JSON data set to a specified file descriptor.
Definition json_lexer.h:1603
bool CurrentBool()
Returns true if the current element a TRUE_EL primitive type.
Definition json_lexer.h:1127
JsonRef next()
Traverse: check for ref validity and increment the position.
Definition json_lexer.h:798
json_primitive_type GetNextNameInCurrentArray()
Get the next element of type NAME that exists in the current ARRAY after the current position.
Definition json_lexer.h:917
ParsedJsonDataSet * AddArrayElement(const IPADDR &i)
Add an IP address to the current array.
ParsedJsonDataSet * AddObjectStart(const char *name)
Use to start an object in the JSON data set.
ParsedJsonDataSet * StartBuilding()
Use to start building the JSON data set.
int FindAndCopyGlobalString(const char *name, char *buf, int buflen)
Starts at the current position and then starts again at the beginning of the data set if the element ...
Definition json_lexer.h:1369
ParsedJsonDataSet * AddArrayElementArray()
Add the start of an array element to the current array.
const char * FindGlobalString(const char *name)
Find the string value of a given element. Starts at the current position and then starts again at the...
Definition json_lexer.h:1349
JsonRef start()
Begin traversal: check for ref validity and reset the position.
Definition json_lexer.h:792
json_primitive_type GetNextName()
Iterates the current element of the JSON data set until it finds one of type NAME.
Definition json_lexer.h:873
ParsedJsonDataSet * DoneBuilding()
Add an end JSON data set and finish building.
ParsedJsonDataSet * EndArray()
Add an ARRAY end to the JSON data set.
const char * FindString(const char *name)
Find the string value of the element after the element with the given name.
Definition json_lexer.h:1404
ParsedJsonDataSet * AddArrayElement(short i)
Add a short value to the current array.
double CurrentNumber()
Get the number value of the current element.
Definition json_lexer.h:1143
int CopyCurrentString(NBString &nbs)
Copy the string value of the current element.
Definition json_lexer.h:1167
ParsedJsonDataSet * Add(const char *name, long i)
Add a name/value pair to the JSON data set where the value is a long.
int FindAndCopyFullNameString(const char *name, char *buf, int buflen)
Find and copy the string value of the element with the given name in the current object....
Definition json_lexer.h:1316
ParsedJsonDataSet * AddArrayElement(unsigned long i)
Add an unsigned long value to the current array.
int PrintObject(bool pretty=false)
Prints the JSON data set to stdout.
Definition json_lexer.h:1583
JsonRef operator()(const char *name)
Get a JsonRef representing any entity from a parent object by key name.
Definition json_lexer.h:755
void DumpState()
Outputs what's in the parse tree to stdout.
void EnableLargeStrings(bool b)
Call to allow allocation of strings larger than 1500 bytes.
Definition json_lexer.h:659
bool FindObjectInCurentObject(const char *name)
Find the OBJECT of the element with the given name in the current OBJECT. Does not search sub-objects...
Definition json_lexer.h:1552
json_primitive_type GetNext()
Get the element at the next position of the JSON data set.
Definition json_lexer.h:828
int PrintObjectToString(NBString &s, bool pretty=false)
Prints the JSON data set to a NBString object.
Definition json_lexer.h:1613
ParsedJsonDataSet * Add(const char *name, bool b)
Add a name/value pair to the JSON data set where the value is a bool.
ParsedJsonDataSet * AddArrayElement(double d)
Add a double to the current array.
ParsedJsonDataSet * AddArrayElement(long i)
Add a long value to the current array.
double FindFullNameNumber(const char *name)
Find the number value of the element with the given name in the current object.
Definition json_lexer.h:1468
ParsedJsonDataSet * AddArrayElement(int i)
Add an integer value to the current array.
bool FindObject(const char *name)
Find the OBJECT of the element after the element with the given name.
Definition json_lexer.h:1538
ParsedJsonDataSet * Add(const char *name, const IPADDR &i)
Add a name/value pair to the JSON data set where the value is an IP address.
bool CopyObject(ParsedJsonDataSet &src_set)
Copies the provided JSON data set into the current one.
int CopyCurrentString(char *buf, int buflen)
Copy the string value of the current element.
Definition json_lexer.h:1159
double FindNumber(const char *name)
Find the number value of the element after the element with the given name.
Definition json_lexer.h:1495
const char * FindStringInCurentObject(const char *name)
Find the string value of the element with the given name in the current object. Does not search sub-o...
Definition json_lexer.h:1454
const char * FindFullNameString(const char *name)
Find the string value of the element with the given name in the current object.
Definition json_lexer.h:1299
void ClearObject()
Clears all data from the object and resets it to its initial state.
int FindAndCopyGlobalString(const char *name, NBString &nbs)
Starts at the current position and then starts again at the beginning of the data set if the element ...
Definition json_lexer.h:1389
JsonRef operator[](int i)
Get a JsonRef from an array by numerical index.
Definition json_lexer.h:742
Base class for web client response buffers.
Definition web_buffers.h:20
json_primitive_type
The following types define the basic building blocks that make up a JSON data set....
Definition json_lexer.h:60
void CharOutputFn(const char *chars, int len, void *blob)
Helper function typedef for print functions.
Definition json_lexer.h:92
@ BEGIN_ARRAY
[ - Signals the start of an array.
Definition json_lexer.h:62
@ NUMBER
An element with a number value.
Definition json_lexer.h:69
@ EOF_EL
Last token in the data set.
Definition json_lexer.h:77
@ TRUE_EL
An element with the value true.
Definition json_lexer.h:71
@ STRING_CHUNK
Not quiet an error, but we got a sting but it was too big to fit in the storage scheme (1500 bytes).
Definition json_lexer.h:74
@ NOTFOUND
Return value when we don't find what we are looking for.
Definition json_lexer.h:76
@ UNDEFINED
Not a defined primitive type. This indicates an error somewhere.
Definition json_lexer.h:61
@ NAME
"xxx": - Signals the name of a name/value pair.
Definition json_lexer.h:66
@ END_OBJECT
} - Signals the end of an object.
Definition json_lexer.h:65
@ NULL_EL
An element with the null value.
Definition json_lexer.h:72
@ ALLOC_STRING
We allocated a large string.
Definition json_lexer.h:75
@ BEGIN_OBJECT
{ - Signals the start of an object.
Definition json_lexer.h:63
@ END_ARRAY
] - Signals the end of an array.
Definition json_lexer.h:64
@ STRING_TOO_BIG
Error, we got a sting but it was too big to fit in the storage scheme (1500 bytes).
Definition json_lexer.h:73
@ VALUE_SEPERATOR
, - Signals the separation between to arrays, objects, or values.
Definition json_lexer.h:68
@ STRING
"xxx" - An element with a string value.
Definition json_lexer.h:67
@ FALSE_EL
An element with the value false.
Definition json_lexer.h:70
NetBurner IPADDR4 Class. See the IPADDR4 Class page for complete documentation.
A list of large strings that are created with malloc.
Definition json_lexer.h:99
JsonAllocString * pNext
A pointer to the next string.
Definition json_lexer.h:100
char data[]
The current string data.
Definition json_lexer.h:101
Main buffer structure for network and serial communication.
Definition buffers.h:90