DynamicRest.JsonReader.ReadValue C# (CSharp) Method

ReadValue() public method

public ReadValue ( ) : object
return object
        public object ReadValue()
        {
            object value = null;
            bool allowNull = false;

            char ch = PeekNextSignificantCharacter();
            if (ch == '[') {
                value = ReadArray();
            }
            else if (ch == '{') {
                value = ReadObject();
            }
            else if ((ch == '\'') || (ch == '"')) {
                bool hasLeadingSlash;
                string s = ReadString(out hasLeadingSlash);
                if (hasLeadingSlash && s.StartsWith("@") && s.EndsWith("@")) {
                    long ticks;

                    if (Int64.TryParse(s.Substring(1, s.Length - 2), NumberStyles.Any, CultureInfo.InvariantCulture, out ticks)) {
                        value = new DateTime(ticks * 10000 + JsonReader.MinDateTimeTicks, DateTimeKind.Utc);
                    }
                }

                if (value == null) {
                    value = s;
                }
            }
            else if (Char.IsDigit(ch) || (ch == '-') || (ch == '.')) {
                value = ReadNumber();
            }
            else if ((ch == 't') || (ch == 'f')) {
                value = ReadBoolean();
            }
            else if (ch == 'n') {
                ReadNull();
                allowNull = true;
            }

            if ((value == null) && (allowNull == false)) {
                throw new FormatException("Invalid JSON text.");
            }
            return value;
        }

Usage Example

コード例 #1
0
ファイル: JsonSample.cs プロジェクト: ioulia/dynamicrest
        public static void Run()
        {
            string jsonText = "{ xyz: 123, items: [ 10, 100, 1000 ], numbers: [ 0.123, .456 ], bools: [ true, false ], text: [ \"hello\", 'world\n!' ] }";

            JsonReader jsonReader = new JsonReader(jsonText);
            dynamic jsonObject = jsonReader.ReadValue();
            dynamic items = jsonObject.items;

            items[2] = 1001;

            dynamic bar = new JsonObject();
            bar.name = "c#";

            jsonObject.bar = bar;

            JsonWriter writer = new JsonWriter();
            writer.WriteValue((object)jsonObject);

            string newJsonText = writer.Json;

            Console.WriteLine(newJsonText);
        }
All Usage Examples Of DynamicRest.JsonReader::ReadValue