Ejemplo n.º 1
0
        internal static GenericDataObject JTokenToGenericData(JToken token)
        {
            switch (token.Type)
            {
            case JTokenType.Object:
                var jobject = (IEnumerable <KeyValuePair <string, JToken> >)token;
                return(jobject.ToDictionary(field => field.Key, field => JTokenToGenericData(field.Value)));

            case JTokenType.Array:
                return(GenericDataList.Create(((JArray)token).Select(item => JTokenToGenericData(item))));

            case JTokenType.Integer:
                return((long)token);

            case JTokenType.Float:
                return((double)token);

            case JTokenType.String:
                return((string)token);

            case JTokenType.Boolean:
                return((bool)token);

            case JTokenType.Null:
                return(null);

            // Currently unsupported types.
            case JTokenType.None:
            case JTokenType.Constructor:
            case JTokenType.Property:
            case JTokenType.Comment:
            case JTokenType.Undefined:
            case JTokenType.Date:
            case JTokenType.Raw:
            case JTokenType.Bytes:
            case JTokenType.Guid:
            case JTokenType.Uri:
            case JTokenType.TimeSpan:
                throw new InvalidOperationException($"Unsupported JSON token type: {token.Type}");

            default:
                throw new InvalidOperationException("Unreachable code executed.");
            }
        }
Ejemplo n.º 2
0
            internal GenericDataObject ToGenericDataObject()
            {
                switch (Kind)
                {
                case JsonValueKind.Object:

                    var obj  = AsObject().Value;
                    var dict = new Dictionary <string, GenericDataObject>();
                    foreach (var property in obj)
                    {
                        dict.Add(property.Key, property.Value.ToGenericDataObject());
                    }

                    return(GenericDataMap.Create(dict));

                case JsonValueKind.Array:
                    return(GenericDataList.Create(AsArray().Value.Select <GenericDataObject>(elem => elem.ToGenericDataObject())));

                case JsonValueKind.String:
                    return(GenericDataString.Create(AsString()));

                case JsonValueKind.Number:
                    return(GenericDataNumber.Create(AsDouble().Value));

                case JsonValueKind.True:
                    return(GenericDataBool.True);

                case JsonValueKind.False:
                    return(GenericDataBool.False);

                case JsonValueKind.Null:
                    return(null);

                default:
                    throw Exceptions.Unreachable;
                }
            }