public bool Parse( string jsonFragment, out string jsonRemainder ) { jsonRemainder = StringUtils.StripLeadingJsonWhitespace( jsonFragment ); // First character now should be " if ( !( StringUtils.GetFirstCharacter( jsonRemainder ) == "\"" ) ) { // If it is not, then fail (bad formatting) Success = false; jsonRemainder = jsonFragment; return(Success); } // Remove " jsonRemainder = StringUtils.StripFirstCharacter( jsonRemainder ); /* * Find closing quotation mark * Note that it can be escaped with a \ * so we need to check for this also */ bool isEscaped = false; int i = 0; /* * Conditions to continue loop * Haven't reached the end of the string * ^ checked in while loop condition * also * we haven't reached a quotation mark * or * we have reached a quotation mark and it is escaped * ^ these two checked in while loop body, use break statement */ while (i < jsonRemainder.Length) { if (jsonRemainder.Substring(i, 1) == "\\") { // When there is a slash, flip whether we are escaped isEscaped = !isEscaped; } else { // If unescaped " , done, exit loop if ( !isEscaped && jsonRemainder.Substring(i, 1) == "\"" ) { break; } // If not, reset escaped count isEscaped = false; } i++; } // If we couldn't find the closing quote if (i == jsonRemainder.Length) { Success = false; jsonRemainder = jsonFragment; return(Success); } // Extract the string up to just before the " value = jsonRemainder.Substring(0, i); // Check all escape sequences, replace if valid if ( !ReplaceEscaped( value, out value ) ) { // If any escape sequences were invalid Success = false; jsonRemainder = jsonFragment; return(Success); } // Return the rest of the json string after the closing " jsonRemainder = jsonRemainder.Substring(i + 1); Success = true; return(Success); }
public bool Parse( string jsonFragment, out string jsonRemainder ) { jsonRemainder = StringUtils.StripLeadingJsonWhitespace( jsonFragment ); // The first character must be { for an array if ( StringUtils.GetFirstCharacter( jsonRemainder ) != "{" ) { Success = false; jsonRemainder = jsonFragment; return(Success); } // Remove the bracket character jsonRemainder = StringUtils.StripFirstCharacter( jsonRemainder ); /* * Now should have a series of json elements follow by commas * except for the last json element which is followed by ] * * Conditions * string still has some characters left in it * we haven't reached the array closing bracket */ values = new Dictionary <string, IJsonElement>(); IJsonElement nextKey; IJsonElement nextElement; while ( (jsonRemainder.Length > 0) && ( StringUtils.GetFirstCharacter( jsonRemainder ) != "}" ) ) { jsonRemainder = StringUtils.StripLeadingJsonWhitespace( jsonRemainder ); // First try to read in the next key nextKey = SimpleJsonParser.ParseOne( jsonRemainder, out jsonRemainder ); // This must be a string, or invalid json if ( (nextKey == null) || !nextKey.IsString() ) { Success = false; jsonRemainder = jsonFragment; return(Success); } // Should be whitespace : whitespace between key & value jsonRemainder = StringUtils.StripLeadingJsonWhitespace( jsonRemainder ); // If no colon, invalid json, fail if ( StringUtils.GetFirstCharacter( jsonRemainder ) != ":" ) { Success = false; jsonRemainder = jsonFragment; return(Success); } // Step over colon & whitespace after colon jsonRemainder = StringUtils.StripFirstCharacter( jsonRemainder ); jsonRemainder = StringUtils.StripLeadingJsonWhitespace( jsonRemainder ); // Try to extract element nextElement = SimpleJsonParser.ParseOne( jsonRemainder, out jsonRemainder ); // A returned null means that the json was not parseable if (nextElement == null) { Success = false; jsonRemainder = jsonFragment; return(Success); } // Parsed successfully -> add to dictionary of parsed elements values[ nextKey.AsString() ] = nextElement; // Remove leading whitespace jsonRemainder = StringUtils.StripLeadingJsonWhitespace( jsonRemainder ); // Next character should be either comma or bracket } if ( (jsonRemainder.Length > 0) && ( StringUtils.GetFirstCharacter( jsonRemainder ) == "," ) ) { // If it's a comma, remove it jsonRemainder = StringUtils.StripFirstCharacter( jsonRemainder ); } else if ( (jsonRemainder.Length > 0) && ( StringUtils.GetFirstCharacter( jsonRemainder ) == "}" ) ) { /* * For a closing bracket do nothing * It will be caught on next loop condition check */ } else { // If it's anything else, bad formatting Success = false; jsonRemainder = jsonFragment; return(Success); } } // Landing here should mean success Success = true; // Remember to remove closing bracket jsonRemainder = StringUtils.StripFirstCharacter( jsonRemainder ); return(Success); }
public bool Parse( string jsonFragment, out string jsonRemainder ) { jsonRemainder = StringUtils.StripLeadingJsonWhitespace( jsonFragment ); // The first character must be [ for an array if ( StringUtils.GetFirstCharacter( jsonRemainder ) != "[" ) { Success = false; jsonRemainder = jsonFragment; return(Success); } // Remove the bracket character jsonRemainder = StringUtils.StripFirstCharacter( jsonRemainder ); /* * Now should have a series of json elements follow by commas * except for the last json element which is followed by ] * * Conditions * string still has some characters left in it * we haven't reached the array closing bracket */ LinkedList <IJsonElement> elements = new LinkedList <IJsonElement>(); IJsonElement nextElement; while ( (jsonRemainder.Length > 0) && ( StringUtils.GetFirstCharacter( jsonRemainder ) != "]" ) ) { jsonRemainder = StringUtils.StripLeadingJsonWhitespace( jsonRemainder ); nextElement = SimpleJsonParser.ParseOne( jsonRemainder, out jsonRemainder ); // A returned null means that the json was not parseable if (nextElement == null) { Success = false; jsonRemainder = jsonFragment; return(Success); } // Parsed successfully -> add to list of parsed elements elements.AddLast( nextElement ); // Remove leading whitespace jsonRemainder = StringUtils.StripLeadingJsonWhitespace( jsonRemainder ); // Next character should be either comma or bracket if ( (jsonRemainder.Length > 0) && ( StringUtils.GetFirstCharacter( jsonRemainder ) == "," ) ) { // If it's a comma, remove it jsonRemainder = StringUtils.StripFirstCharacter( jsonRemainder ); } else if ( (jsonRemainder.Length > 0) && ( StringUtils.GetFirstCharacter( jsonRemainder ) == "]" ) ) { /* * For a closing bracket do nothing * It will be caught on next loop condition check */ } else { // If it's anything else, bad formatting Success = false; jsonRemainder = jsonFragment; return(Success); } } // Landing here should mean success Success = true; // Move linked list elements to values array values = new IJsonElement[elements.Count]; elements.CopyTo(values, 0); // Remember to remove the closing bracket jsonRemainder = StringUtils.StripFirstCharacter( jsonRemainder ); return(Success); }