public int explore(string json, int from, int to, JsonExploreListener listener, string currentPropertyName = null) { int pos = from; for (; pos <= to; pos++) { int?nextPos = json.firstCharPosition(cin => cin != ',' && !Char.IsWhiteSpace(cin), pos, to);; //firstNonWhiteSpaceCharacterIndex(json, pos, to); if (nextPos == null) { string strError = String.Format("expected whitespace char after char {0}", pos); throw new Exception(strError); } pos = nextPos.Value; char c = json[pos]; if (c == '}') { listener.JsonEndObject(pos); return(pos); } // read a property Name int?propertyNameEnd = json.firstCharPosition(cin => cin == ':', pos, to); if (null == propertyNameEnd) { throw new Exception(string.Format("no end found to property name at char {0} property starts with {1} in json {2}", pos, json.Substring(pos), json.Substring(from))); } currentPropertyName = json.Substring(pos, propertyNameEnd.Value - pos); if (currentPropertyName[0] == '"') { currentPropertyName = currentPropertyName.Substring(1); } if (currentPropertyName.EndsWith("\"")) { currentPropertyName = currentPropertyName.Substring(0, currentPropertyName.Length - 1); } pos = propertyNameEnd.Value + 1; int?valueStartPos = firstNonWhiteSpaceCharacterIndex(json, pos, to); if (valueStartPos == null) { string strError = String.Format("no value found for property {0} at position {1} in json {2}", currentPropertyName, pos, json); throw new Exception(strError); } c = json[valueStartPos.Value]; if (c == ObjectStart) { listener.JsonStartObject(currentPropertyName, valueStartPos.Value); pos = explore(json, valueStartPos.Value + 1, to, listener, currentPropertyName); } else // read a non object { pos = readNonObject(json, valueStartPos.Value, to, listener, currentPropertyName); } } return(pos); }
public void explore(string json, JsonExploreListener listener) { int? firstNWSCharIndex = firstNonWhiteSpaceCharacterIndex(json); int? lastNonWSCharIndex = lastNonWhiteSpaceCharacterIndex(json); if (firstNWSCharIndex == null || lastNonWSCharIndex == null || json[firstNWSCharIndex.Value] != ObjectStart || json[lastNonWSCharIndex.Value] != ObjectEnd) { throw new Exception(String.Format("expected json to start with {0} and end with {1}", ObjectStart, ObjectEnd)); } listener.JsonStartObject(null, 0); explore(json, firstNWSCharIndex.Value + 1, lastNonWSCharIndex.Value, listener); }
public void explore(string json, JsonExploreListener listener) { int?firstNWSCharIndex = firstNonWhiteSpaceCharacterIndex(json); int?lastNonWSCharIndex = lastNonWhiteSpaceCharacterIndex(json); if (firstNWSCharIndex == null || lastNonWSCharIndex == null || json[firstNWSCharIndex.Value] != ObjectStart || json[lastNonWSCharIndex.Value] != ObjectEnd) { throw new Exception(String.Format("expected json to start with {0} and end with {1}", ObjectStart, ObjectEnd)); } listener.JsonStartObject(null, 0); explore(json, firstNWSCharIndex.Value + 1, lastNonWSCharIndex.Value, listener); }
internal int exploreList(string json, int from, int to, JsonExploreListener listener, char terminatingChar, Func <int, bool> terminate, string listType) { // read arguments for (int pos = from + 1; pos <= to; pos++) { int?nextPos = json.firstCharPosition(cin => !Char.IsWhiteSpace(cin), pos, to); if (nextPos == null) { string strError = String.Format("expected whitespace char after char {0}", pos); throw new Exception(strError); } pos = nextPos.Value; char c = json[pos]; if (c == ',') { //throw new Exception("multiple javascript list items not supported"); continue; } if (c == terminatingChar) { //listener.JsonEndFunction(pos); terminate(pos); return(pos); } else if (c == ObjectStart) { listener.JsonStartObject(/*currentPropertyName*/ null, pos); pos = explore(json, pos + 1, to, listener, null /*currentPropertyName*/); } else // process a leaf ! { pos = readNonObject(json, pos, to, listener, null); } } throw new Exception(listType + " not terminated"); }
public int explore(string json, int from, int to, JsonExploreListener listener, string currentPropertyName = null) { int pos = from; for (; pos <= to; pos++) { int? nextPos = json.firstCharPosition(cin => cin != ',' && !Char.IsWhiteSpace(cin), pos, to); ;//firstNonWhiteSpaceCharacterIndex(json, pos, to); if (nextPos == null) { string strError = String.Format("expected whitespace char after char {0}", pos); throw new Exception(strError); } pos = nextPos.Value; char c = json[pos]; if (c == '}') { listener.JsonEndObject(pos); return pos; } // read a property Name int? propertyNameEnd = json.firstCharPosition(cin => cin == ':', pos, to); if (null == propertyNameEnd) { throw new Exception(string.Format("no end found to property name at char {0} property starts with {1} in json {2}", pos, json.Substring(pos), json.Substring(from))); } currentPropertyName = json.Substring(pos, propertyNameEnd.Value - pos); if (currentPropertyName[0] == '"') currentPropertyName = currentPropertyName.Substring(1); if (currentPropertyName.EndsWith("\"")) currentPropertyName = currentPropertyName.Substring(0, currentPropertyName.Length - 1); pos = propertyNameEnd.Value + 1; int? valueStartPos = firstNonWhiteSpaceCharacterIndex(json, pos, to); if (valueStartPos == null) { string strError = String.Format("no value found for property {0} at position {1} in json {2}", currentPropertyName, pos, json); throw new Exception(strError); } c = json[valueStartPos.Value]; if (c == ObjectStart) { listener.JsonStartObject(currentPropertyName, valueStartPos.Value); pos = explore(json, valueStartPos.Value + 1, to, listener, currentPropertyName); } else // read a non object { pos = readNonObject(json, valueStartPos.Value, to, listener, currentPropertyName); } } return pos; }
internal int exploreList(string json, int from, int to, JsonExploreListener listener, char terminatingChar, Func<int, bool> terminate, string listType) { // read arguments for (int pos = from+1; pos <= to; pos++) { int? nextPos = json.firstCharPosition(cin => !Char.IsWhiteSpace(cin), pos, to); if (nextPos == null) { string strError = String.Format("expected whitespace char after char {0}", pos); throw new Exception(strError); } pos = nextPos.Value; char c = json[pos]; if (c==',') { //throw new Exception("multiple javascript list items not supported"); continue; } if (c == terminatingChar) { //listener.JsonEndFunction(pos); terminate(pos); return pos; } else if (c == ObjectStart) { listener.JsonStartObject(/*currentPropertyName*/null, pos); pos = explore(json, pos + 1, to, listener, null/*currentPropertyName*/); } else // process a leaf ! { pos = readNonObject(json, pos, to, listener, null); } } throw new Exception(listType + " not terminated"); }