private static string ScalerJson(ScalerRecord record, int indent, bool ignoreFirstIndent) { StringBuilder builder = new StringBuilder(capacity: 64); if (ignoreFirstIndent == false) { builder.AppendIndent(indent); } switch (record.ScalerType) { case ScalerType.Null: builder.Append("null"); break; case ScalerType.String: builder.AppendFormat("\"{0}\"", record.Value); break; case ScalerType.Decimal: case ScalerType.Double: builder.Append(record.Value.ToString()); break; case ScalerType.Boolean: builder.Append(record.Value.ToString().ToLower()); break; default: throw new NotImplementedException(); } return(builder.ToString()); }
public void TestFromJson() { decimal d = 1234; long v = (long)d; Stream file = File.OpenRead("data.json"); RecordBase record = Converter.ReadJson(file); ScalerRecord scaler = (ScalerRecord)record.RecordFromPath("[5].abc"); long value = scaler.ReadAs <long>(); file.Dispose(); string json = record.Json(); }
private static RecordBase ReadJson(InternalTextReader reader, bool isCalledByList, bool ignoreKeyCase) { char peek; int state = 1; while (true) { if (!reader.HasChar) { throw new Exception("unexcepted end of stream"); } peek = (char)reader.Peek(); if (state == 1) { if (peek.IsWhiteSpace()) { reader.Read(); } else if (peek == '[') { state = 2; } else if (peek == '{') { state = 3; } else if (peek == 't') { state = 4; } else if (peek == 'f') { state = 5; } else if (peek == 'n') { state = 6; } else if (peek == '"') { state = 7; } else if (peek.IsNumber()) { state = 8; } else if (peek == '-') { state = 9; reader.Read(); } else if (peek == '/') { state = 10; reader.Read(); } else if (peek == ']' && isCalledByList) { reader.Read(); return(null); } else { throw BuildException($"unexcepted char '{peek}'", reader); } } else if (state == 2) { return(ReadList(reader, ignoreKeyCase)); } else if (state == 3) { return(ReadSet(reader, ignoreKeyCase)); } else if (state == 4) { ScanStringOrThrow(reader, "true"); ScalerRecord record = new ScalerRecord(true); return(record); } else if (state == 5) { ScanStringOrThrow(reader, "false"); ScalerRecord record = new ScalerRecord(false); return(record); } else if (state == 6) { ScanStringOrThrow(reader, "null"); ScalerRecord record = new ScalerRecord(null); return(record); } else if (state == 7) { ScalerRecord record = new ScalerRecord(ReadStringOrThrow(reader)); return(record); } else if (state == 8) { ScalerRecord record = new ScalerRecord(ReadNumberOrThrow(reader)); return(record); } else if (state == 9) { if (peek.IsNumber()) { object val = ReadNumberOrThrow(reader); if (val is int intval) { val = -intval; } else if (val is double doubleval) { val = -doubleval; } ScalerRecord record = new ScalerRecord(val); return(record); } else { throw BuildException($"unexcepted char '{peek}', number expected", reader); } } else if (state == 10) { if (peek == '/') { state = 11; reader.Read(); } else { throw BuildException($"unexcepted char '{peek}'", reader); } } else if (state == 11) { if (peek == '\n') { state = 1; reader.Read(); } else { reader.Read(); } } else { throw new Exception($"unknow state of {state}"); } } }