Ejemplo n.º 1
0
        public void ReadValue(JsonAction <object, JsonValueType> value, JsonAction readArray, JsonAction readObject)
        {
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (readArray is null)
            {
                throw new ArgumentNullException(nameof(readArray));
            }
            if (readObject is null)
            {
                throw new ArgumentNullException(nameof(readObject));
            }
            switch (Values.Peek().Type)
            {
            case JsonValueType.Array:
                readArray();
                return;

            case JsonValueType.Object:
                readObject();
                return;
            }
            if (JsonApi.HasFlag(Values.Peek().Type, JsonValueType.Value))
            {
                value(Values.Peek().Value, Values.Peek().Type);
                return;
            }
            throw new JsonNotSupportException(Values.Peek().Type);
        }
Ejemplo n.º 2
0
        internal void Write(object value, JsonValueType valueType)
        {
            CheckEnd();
            Head();
            if (JsonApi.HasFlag(Stacks.Peek().Stage, JsonTextWriterStage.Key))
            {
                valueType = JsonValueType.String;
            }
            switch (valueType)
            {
            case JsonValueType.Null:
                TextWriter.Write(JsonKeyword.Null);
                break;

            case JsonValueType.Integer:
                TextWriter.Write(value);
                break;

            case JsonValueType.Decimal:
                TextWriter.Write(value);
                if (value.ToString().IndexOf('.') < 0)
                {
                    TextWriter.Write(".0");
                }
                break;

            case JsonValueType.Bool:
                TextWriter.Write(Convert.ToBoolean(value) ? JsonKeyword.True : JsonKeyword.False);
                break;

            case JsonValueType.String:
                WriteString(JsonApi.CancelUnescape(Convert.ToString(value)));
                break;

            case JsonValueType.DateTime:
                DateTime dateTime = Convert.ToDateTime(value);
                WriteString(Config.UTCTime ? dateTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ") : dateTime.ToString());
                break;

            default:
                throw new JsonNotSupportException(valueType);
            }
            Tail();
        }