public void DumpValueIterative(StreamWriter print2, JsonNode o, Byte[] src, int indent = 0) { String space, newLine; if (indent > -1) { space = " "; newLine = "\n"; } else { space = ""; newLine = ""; } JsonTag startTag; do { #if DEBUGGING VisualNode3 oV = new VisualNode3(ref o, src, 10000); #endif if (indent > -1) { print2.Write(new String(' ', indent)); // Start with indent } startTag = o.Tag; if (startTag == JsonTag.JSON_OBJECT || startTag == JsonTag.JSON_ARRAY) { String open = ""; if (startTag == JsonTag.JSON_ARRAY) { open = "[]"; } else { open = "{}"; } if (o.ToNode() == null) { if (o.HasKey) { print2.Write($"\"{o.Key(src)}\":{space}"); // [] or key: [] } if (o.next == null) { print2.Write($"{open}{newLine}"); } else { print2.Write($"{open},{newLine}"); } if (o.next == null) { o = o.node; } } else { open = open.Substring(0, 1); if (o.HasKey) { print2.Write($"\"{o.Key(src)}\":{space}{open}"); } else { print2.Write($"{open}"); } if (o.ToNode() != null) { print2.Write(newLine); } if (o.ToNode() == null && o.next != null) { BlockEnd(print2, o, ref indent, newLine); } if (indent > -1) { indent += SHIFT_WIDTH; } } } else if (startTag == JsonTag.JSON_STRING || startTag == JsonTag.JSON_NUMBER || startTag == JsonTag.JSON_NUMBER_STR) { String quote = (startTag == JsonTag.JSON_STRING) ? "\"" : ""; if (o.HasKey) { print2.Write($"\"{o.Key(src)}\":{space}{quote}{o.ToString(src)}{quote}{(o.next!=null?",":"")}{newLine}"); // "key": "value"(,) } else { print2.Write($"{quote}{o.ToString(src)}{quote}{(o.next!=null?",":"")}{newLine}"); // "value"(,) } } else if (startTag == JsonTag.JSON_TRUE || startTag == JsonTag.JSON_FALSE || startTag == JsonTag.JSON_NULL) { String word; if (startTag == JsonTag.JSON_TRUE) { word = "true"; } else if (startTag == JsonTag.JSON_FALSE) { word = "false"; } else { word = "null"; } if (o.HasKey) { print2.Write($"\"{o.Key(src)}\":{space}{word}{(o.next!=null?",":"")}{newLine}"); // "key": "value"(,) } else { print2.Write($"{word}{(o.next!=null?",":"")}{newLine}"); // "value"(,) } } if (o != null) { if (o.node != null && (startTag == JsonTag.JSON_ARRAY || startTag == JsonTag.JSON_OBJECT)) { // move down 2 node of structured object #if DEBUGGING levelStack.Push(new VisualNode3(ref o, src, 1000)); #else levelStack.Push(o); #endif o = o.node; } else // move right to values { if (o.next != null) { o = o.next; } else { o = o.node; // always null (4 null || non-structured) } } } while (o == null && levelStack.Count > 0) { // return back after iterations do { #if DEBUGGING o = levelStack.Pop().m_JsonNode; oV.ChangeNode(o); #else o = levelStack.Pop(); #endif if (o.Tag == JsonTag.JSON_ARRAY || o.Tag == JsonTag.JSON_OBJECT) { // Array / Object end markers BlockEnd(print2, o, ref indent, newLine); } else { BlockEnd(print2, o, ref indent, newLine); // Array / Object end markers } } while ((levelStack.Count > 1) && ((o == null || (o.next == null && (o.node == null || o.node.next == null))))); o = o.next; // move right } } while (o != null || (levelStack.Count > 0)); }
public DebugVisual(JsonNode _me, int _arround, Byte[] _src) { src = _src; arround = _arround; update(_me, _arround); }
public void DumpValue(StreamWriter print2, JsonNode o, Byte[] src, int indent = 0) { JsonNode i; if (o.Tag == JsonTag.JSON_NUMBER) { print2.Write(o.ToNumber().ToString(System.Globalization.CultureInfo.InvariantCulture)); } else if (o.Tag == JsonTag.JSON_NUMBER_STR) { print2.Write(o.ToString(src)); } else if (o.Tag == JsonTag.JSON_STRING) { print2.Write($"\"{ o.ToString(src) }\""); } else if (o.Tag == JsonTag.JSON_ARRAY) { // It is not necessary to use o.toNode() to check if an array or object // is empty before iterating over its members, we do it here to allow // nicer pretty printing. if (null == o.ToNode()) { print2.Write("[]"); return; } if (indent > -1) { print2.Write("[\n"); } else { print2.Write('['); } i = o.node; while (null != i) { if (indent > -1) { print2.Write(new String(' ', indent + SHIFT_WIDTH)); } DumpValue(print2, i, src, indent > -1 ? indent + SHIFT_WIDTH : indent); if (indent > -1) { print2.Write(i.next != null ? ",\n" : "\n"); } else if (i.next != null) { print2.Write(","); } i = i.next; } print2.Write((indent > -1) ? (new String(' ', indent) + ']') : "]"); } else if (o.Tag == JsonTag.JSON_OBJECT) { if (null == o.ToNode()) { print2.Write("{}"); return; } if (indent > -1) { print2.Write("{\n"); } else { print2.Write("{"); } i = o.node; while (null != i) { if (indent > -1) { print2.Write(new String(' ', indent + SHIFT_WIDTH)); } print2.Write($"\"{ i.Key(src) }\""); if (indent > -1) { print2.Write(": "); } else { print2.Write(':'); } DumpValue(print2, i, src, indent > -1 ? indent + SHIFT_WIDTH : indent); if (indent > -1) { print2.Write(i.next != null ? ",\n" : "\n"); } else if (i.next != null) { print2.Write(","); } i = i.next; } print2.Write(((indent > -1) ? new String(' ', indent) : "") + '}'); } else if (o.Tag == JsonTag.JSON_TRUE) { print2.Write("true"); } else if (o.Tag == JsonTag.JSON_FALSE) { print2.Write("false"); } else if (o.Tag == JsonTag.JSON_NULL) { print2.Write("null"); } }
public JsonErrno Parse(Byte[] s, ref int endPos, out JsonNode value #if KEY_SPLIT , ByteString[] keysLog, int level, int startPos, int length #endif ) { int endPosMem = endPos; Init(ref s, (endPos > 0)); value = null; while (strPos < len) { #if !SKIP_VALIDATION if (type > 1) { prevType = type; } #endif type = SearchTables.valTypes[s[strPos]]; if (type <= 1) { strPos++; continue; // white space } endPos = strPos++; switch (type) // switch (**endptr) { { case 2: // case '-': if (FloatAsDecimal) { endPos = o.String2decimal(ref strPos, s, true); } else { endPos = o.String2double(ref strPos, s, true); } #if !SKIP_VALIDATION if (0 == (SearchTables.specialTypes[s[endPos]] & 3)) // isdelim { endPos = strPos; return(JsonErrno.BAD_NUMBER); } #endif break; case 4: // 0-9 strPos--; if (FloatAsDecimal) { endPos = o.String2decimal(ref strPos, s); } else { endPos = o.String2double(ref strPos, s); } #if !SKIP_VALIDATION if (0 == (SearchTables.specialTypes[s[endPos]] & 3)) // isdelim { endPos = strPos; return(JsonErrno.BAD_NUMBER); } #endif break; case 3: // case '"': JsonErrno e = o.GetString(ref strPos, s); if (e != JsonErrno.OK) { return(e); } #if !SKIP_VALIDATION if (0 == (SearchTables.specialTypes[s[strPos]] & 3)) // !isdelim { endPos = strPos; return(JsonErrno.BAD_STRING); } #endif break; case 7: // 't' if ((SearchTables.specialTypes[s[strPos + 3]] & 3) != 0 && // isdelim (s[strPos + 0] == 'r') && (s[strPos + 1] == 'u') && (s[strPos + 2] == 'e')) { o.Tag = JsonTag.JSON_TRUE; strPos += 3; #if !SKIP_VALIDATION } else { return(JsonErrno.BAD_IDENTIFIER); #endif } break; case 6: // 'f' if ((SearchTables.specialTypes[s[strPos + 4]] & 3) != 0 && // isdelim (s[strPos + 0] == 'a') && (s[strPos + 1] == 'l') && (s[strPos + 2] == 's') && (s[strPos + 3] == 'e')) { o.Tag = JsonTag.JSON_FALSE; strPos += 4; #if !SKIP_VALIDATION } else { return(JsonErrno.BAD_IDENTIFIER); #endif } break; case 8: // 'n' #if !SKIP_VALIDATION if (prevType == 3 && !separator) // {"Missing colon" null} + fail19.json { return(JsonErrno.UNEXPECTED_CHARACTER); } #endif if ((SearchTables.specialTypes[s[strPos + 3]] & 3) != 0 && // isdelim (s[strPos + 0] == 'u') && (s[strPos + 1] == 'l') && (s[strPos + 2] == 'l')) { o.Tag = JsonTag.JSON_NULL; strPos += 3; #if !SKIP_VALIDATION } else { return(JsonErrno.BAD_IDENTIFIER); #endif } break; case 12: // ']' #if !SKIP_VALIDATION if (pos == -1) { return(JsonErrno.STACK_UNDERFLOW); } if (tags[pos] != JsonTag.JSON_ARRAY) { return(JsonErrno.MISMATCH_BRACKET); } if (separator && prevType != 11) // '[' { return(JsonErrno.UNEXPECTED_CHARACTER); // fail4 } #endif #if DEBUGGING o.ListToValue(JsonTag.JSON_ARRAY, tails[pos] != null ? tails[pos].m_JsonNode : null); pos--; #else o.ListToValue(JsonTag.JSON_ARRAY, tails[pos--]); #endif #if !SKIP_VALIDATION if (type > 1) { prevType = type; } #endif break; case 13: // '}' #if !SKIP_VALIDATION if (pos == -1) { return(JsonErrno.STACK_UNDERFLOW); } if (tags[pos] != JsonTag.JSON_OBJECT) { return(JsonErrno.MISMATCH_BRACKET); } if (keys[pos].length != -1) { return(JsonErrno.UNEXPECTED_CHARACTER); } if (separator && prevType != 10) // '{' { return(JsonErrno.UNEXPECTED_CHARACTER); } #endif #if DEBUGGING o.ListToValue(JsonTag.JSON_OBJECT, tails[pos] != null ? tails[pos].m_JsonNode : null); pos--; #else o.ListToValue(JsonTag.JSON_OBJECT, tails[pos--]); #endif #if KEY_SPLIT if (insideLimitBlock && (level == pos + 1)) { if (length == 1) { bubbleOut = true; } else if (length > 1) { length--; } } #endif break; case 11: // '[' #if !SKIP_VALIDATION if (++pos == JSON_STACK_SIZE) { return(JsonErrno.STACK_OVERFLOW); } #else pos++; #endif tails[pos] = null; tags[pos] = JsonTag.JSON_ARRAY; keys[pos].length = -1; #if !SKIP_VALIDATION separator = true; #endif continue; case 10: // '{' #if !SKIP_VALIDATION if (++pos == JSON_STACK_SIZE) { return(JsonErrno.STACK_OVERFLOW); } #else pos++; #endif #if KEY_SPLIT if (pos == level && !insideLimitBlock) { int i = level - 1; while (i >= 0) { if (keys[i].length != -1) { #if DEBUGGING if (keysLog[i].Equals(s, keys[i].idxes)) { i--; } #else if (keysLog[i].Equals(s, keys[i])) { i--; } #endif else { break; } } else if (keysLog[i] == null) { i--; } else { break; } } if (i == -1) { // Keys & level match insideLimitBlock = true; if (startPos > 0) { strPos = endPosMem; } } } #endif tails[pos] = null; tags[pos] = JsonTag.JSON_OBJECT; keys[pos].length = -1; #if !SKIP_VALIDATION separator = true; #endif continue; case 14: // ':' #if !SKIP_VALIDATION if (separator || keys[pos].length == -1) { return(JsonErrno.UNEXPECTED_CHARACTER); } separator = true; #endif continue; case 15: // ',' #if !SKIP_VALIDATION if (separator || keys[pos].length != -1) { return(JsonErrno.UNEXPECTED_CHARACTER); } separator = true; #endif continue; #if !SKIP_VALIDATION default: return(JsonErrno.UNEXPECTED_CHARACTER); #endif } #if !SKIP_VALIDATION separator = false; #endif if (pos == -1) { value = o; #if !SKIP_VALIDATION while (strPos < len - 1) { if (type == 13) // '}' / {"Extra value after close": true} "misplaced quoted value" { type = 0; while (strPos < len - 1) { type = SearchTables.valTypes[s[strPos]]; if (type <= 1) { strPos++; continue; } if (type == 3) { strPos++; JsonNode trash = new JsonNode(); JsonErrno e = trash.GetString(ref strPos, s); if (e != JsonErrno.OK) { return(e); } endPos = strPos; if (strPos != len - 1 || 0 == (SearchTables.specialTypes[s[strPos]] & 3)) // !isdelim { return(JsonErrno.BAD_STRING); } else { return(JsonErrno.OK); } } else { return(JsonErrno.UNEXPECTED_CHARACTER); } } } if ((strPos < len - 1) && SearchTables.valTypes[s[strPos]] <= 1) { strPos++; } else { if (s[strPos] == 0) { break; } if (prevType != 12 || s[strPos] != ',') // ']' { return(JsonErrno.BREAKING_BAD); } else { strPos++; } } } #endif return(JsonErrno.OK); } #if KEY_SPLIT do { #endif if (tags[pos] == JsonTag.JSON_OBJECT) { if (keys[pos].length == -1) { #if !SKIP_VALIDATION if (o.Tag != JsonTag.JSON_STRING) { return(JsonErrno.UNQUOTED_KEY); } #endif #if DEBUGGING keys[pos] = new LinkedByteString(o.doubleOrString); #else keys[pos].data = o.doubleOrString.data; #endif #if KEY_SPLIT if (bubbleOut) { continue; } else { break; } #else continue; #endif } #if DEBUGGING o.InsertAfter(tails[pos] != null ? tails[pos].m_JsonNode : null, ref keys[pos].idxes); #else o.InsertAfter(tails[pos] != null ? tails[pos] : null, ref keys[pos]); #endif } else { #if DEBUGGING o.InsertAfter(tails[pos] != null ? tails[pos].m_JsonNode : null); #else o.InsertAfter(tails[pos]); #endif } tails[pos] = #if DEBUGGING new VisualNode3(ref o, s, 3000); #else o; #endif o = new JsonNode(); #if DEBUGGING root.ChangeNode(o); #endif #if KEY_SPLIT if (bubbleOut) { if (tags[pos] == JsonTag.JSON_ARRAY || tags[pos] == JsonTag.JSON_OBJECT) { // lists close brackets #if DEBUGGING o.ListToValue(tags[pos], tails[pos] != null ? tails[pos].m_JsonNode : null); #else o.ListToValue(tags[pos], tails[pos]); #endif } if (pos-- == 0) { while ((strPos < len) && s[strPos++] != ',') { ; // find array separator } while ((strPos < len) && ((SearchTables.specialTypes[s[strPos]] & 3) != 0)) { strPos++; // skip delims } while ((strPos < len) && (s[strPos] != '{')) { strPos++; // array start } if (strPos < len) { strPos++; } endPos = strPos; value = o; return(JsonErrno.OK); } } else { break; } } while (true) { ; // exit by breaks } #endif }
public void ChangeNode(JsonNode o) { m_JsonNode = o; }
public StringBuilder DumpXMLValueIterative(JsonNode o) { JsonNode startNode = o; String newLine; StringBuilder retVal = new StringBuilder(); if (m_Indent > -1) { newLine = "\n"; m_Indent = 0; } else { newLine = ""; } JsonTag startTag; nos = new List <int>(); do { if (m_Indent > -1) { retVal.Append(' ', m_Indent); // Start with m_Indent } startTag = o.Tag; if (startTag == JsonTag.JSON_OBJECT || startTag == JsonTag.JSON_ARRAY) { String open = ""; if (startTag == JsonTag.JSON_ARRAY) { open = "<EmptyArray></EmptyArray>"; } else { open = "<EmptyObject></EmptyObject>"; } if (o.ToNode() == null) { if (o.HasKey) { retVal.Append('<').Append(o.Key(src)).Append('>'); } if (o.next == null) { retVal.Append(open); } else { retVal.Append(open).Append(newLine); } if (o.HasKey) { retVal.Append("</").Append(o.Key(src)).Append(">"); } if (o.next == null) { retVal.Append(newLine); } if (o.next == null) { o = o.node; } } else { if (o.HasKey) { retVal.Append('<').Append(o.Key(src)).Append(">"); } else { if (startNode == o) { nos.Add(1); retVal.Append("<ROOT>"); } else { retVal.Append("<No").Append(nos[nos.Count - 1]).Append('>'); } while (nos.Count < levelStack.Count) { nos.Add(1); } } if (o.ToNode() != null) { retVal.Append(newLine); } if (o.ToNode() == null && o.next != null) { BlockEndXML(o, ref retVal, newLine); } if (m_Indent > -1) { m_Indent += m_Shift_Width; } } } else if (startTag == JsonTag.JSON_STRING || startTag == JsonTag.JSON_NUMBER || startTag == JsonTag.JSON_NUMBER_STR) { String quote = (startTag == JsonTag.JSON_STRING) ? "\"" : ""; if (o.HasKey) { retVal.Append('<').Append(o.Key(src)).Append('>').Append(o.ToString(src)).Append("</").Append(o.Key(src)).Append('>').Append(newLine); // "key": "value"(,) } else { retVal.Append(quote).Append(o.ToString(src)).Append(quote).Append($"{(o.next != null ? "," : "")}{newLine}"); // "value"(,) } } else if (startTag == JsonTag.JSON_TRUE || startTag == JsonTag.JSON_FALSE || startTag == JsonTag.JSON_NULL) { String word; if (startTag == JsonTag.JSON_TRUE) { word = "true"; } else if (startTag == JsonTag.JSON_FALSE) { word = "false"; } else { word = "null"; } if (o.HasKey) { retVal.Append('<').Append(o.Key(src)).Append('>').Append(word).Append("</").Append(o.Key(src)).Append('>').Append(newLine); // "key": "value"(,) } else { retVal.Append(word).Append($"{(o.next != null ? "," : "")}{newLine}"); // "value"(,) } } if (o != null) { if (o.node != null && (startTag == JsonTag.JSON_ARRAY || startTag == JsonTag.JSON_OBJECT)) { // move down 2 node of structured object levelStack.Push(o); o = o.node; } else // move right to values { if (o.next != null) { o = o.next; } else { o = o.node; // always null (4 null || non-structured) } } } while (o == null && levelStack.Count > 0) { // return back after iterations do { o = levelStack.Pop(); if (o.Tag == JsonTag.JSON_ARRAY || o.Tag == JsonTag.JSON_OBJECT) { // Array / Object end markers if (o == startNode) { return(retVal.Append("</ROOT>")); } else { BlockEndXML(o, ref retVal, newLine); } } } while ((levelStack.Count > 1) && ((o == null || (o.next == null && (o.node == null || o.node.next == null))))); o = o.next; // move right } if (o == startNode) { if (m_Indent > -1) { m_Indent = 0; } return(retVal.Append("\n... cycle here")); } } while (o != null || (levelStack.Count > 0)); return(retVal); }
public VisualNode3(ref JsonNode my, Byte[] src, int debugModeLimit) { m_JsonNode = my; this.src = src; m_debugModeLimit = debugModeLimit; }
public String DumpValueIterative(JsonNode o, Boolean debugModeLimit) { JsonNode startNode = o; String space, newLine, retVal = ""; if (m_Indent > -1) { space = " "; newLine = "\n"; } else { space = ""; newLine = ""; } JsonTag startTag; do { if (debugModeLimit && retVal.Length > m_debugModeLimit) { if (m_Indent > -1) { m_Indent = 0; } return(retVal + "\n..."); } if (m_Indent > -1) { retVal += (new String(' ', m_Indent)); // Start with m_Indent } startTag = o.Tag; if (startTag == JsonTag.JSON_OBJECT || startTag == JsonTag.JSON_ARRAY) { String open = ""; if (startTag == JsonTag.JSON_ARRAY) { open = "[]"; } else { open = "{}"; } if (o.ToNode() == null) { if (o.HasKey) { retVal += ($"\"{o.Key(src)}\":{space}"); // [] or key: [] } if (o.next == null) { retVal += ($"{open}{newLine}"); } else { retVal += ($"{open},{newLine}"); } if (o.next == null) { o = o.node; } } else { open = open.Substring(0, 1); if (o.HasKey) { retVal += ($"\"{o.Key(src)}\":{space}{open}"); } else { retVal += ($"{open}"); } if (o.ToNode() != null) { retVal += (newLine); } if (o.ToNode() == null && o.next != null) { BlockEnd(o, ref retVal, newLine); } if (m_Indent > -1) { m_Indent += m_Shift_Width; } } } else if (startTag == JsonTag.JSON_STRING || startTag == JsonTag.JSON_NUMBER || startTag == JsonTag.JSON_NUMBER_STR) { String quote = (startTag == JsonTag.JSON_STRING) ? "\"" : ""; if (o.HasKey) { retVal += ($"\"{o.Key(src)}\":{space}{quote}{o.ToString(src)}{quote}{(o.next != null ? "," : "")}{newLine}"); // "key": "value"(,) } else { retVal += ($"{quote}{o.ToString(src)}{quote}{(o.next != null ? "," : "")}{newLine}"); // "value"(,) } } else if (startTag == JsonTag.JSON_TRUE || startTag == JsonTag.JSON_FALSE || startTag == JsonTag.JSON_NULL) { String word; if (startTag == JsonTag.JSON_TRUE) { word = "true"; } else if (startTag == JsonTag.JSON_FALSE) { word = "false"; } else { word = "null"; } if (o.HasKey) { retVal += ($"\"{o.Key(src)}\":{space}{word}{(o.next != null ? "," : "")}{newLine}"); // "key": "value"(,) } else { retVal += ($"{word}{(o.next != null ? "," : "")}{newLine}"); // "value"(,) } } if (o != null) { if (o.node != null && (startTag == JsonTag.JSON_ARRAY || startTag == JsonTag.JSON_OBJECT)) { // move down 2 node of structured object levelStack.Push(o); o = o.node; } else // move right to values { if (o.next != null) { o = o.next; } else { o = o.node; // always null (4 null || non-structured) } } } while (o == null && levelStack.Count > 0) { // return back after iterations do { o = levelStack.Pop(); if (o.Tag == JsonTag.JSON_ARRAY || o.Tag == JsonTag.JSON_OBJECT) { // Array / Object end markers BlockEnd(o, ref retVal, newLine); } else { BlockEnd(o, ref retVal, newLine); // Array / Object end markers } } while ((levelStack.Count > 1) && ((o == null || (o.next == null && (o.node == null || o.node.next == null))))); o = o.next; // move right } if (o == startNode) { if (m_Indent > -1) { m_Indent = 0; } return(retVal + "\n... cycle here"); } } while (o != null || (levelStack.Count > 0)); return(retVal); }