Beispiel #1
0
        private void Process(out JsonObject jsonObj, IJsonValue jsonData)
        {
            switch (jsonData.ValueType)
            {
            case JsonValueType.Null:
                jsonObj = new JsonObject();
                break;

            case JsonValueType.Boolean:
                jsonObj = new JsonObject(jsonData.GetBoolean());
                break;

            case JsonValueType.Number:
                jsonObj = new JsonObject(jsonData.GetNumber());
                break;

            case JsonValueType.String:
                jsonObj = new JsonObject(jsonData.GetString());
                break;

            case JsonValueType.Array:
                jsonObj = new JsonObject(JsonObjectType.Array);
                ProcessArray(jsonObj, jsonData.GetArray());
                break;

            case JsonValueType.Object:
                jsonObj = new JsonObject(JsonObjectType.Object);
                ProcessObject(jsonObj, jsonData.GetObject());
                break;

            default:
                throw new Exception("意外情况");
            }
        }
        public object ConvertFromJson(IJsonValue value)
        {
            if (value == null || value.ValueType == JsonValueType.Null)
            {
                return(null);
            }
            else
            {
                JsonArray     array  = value.GetArray();
                ComplexType[] result = new ComplexType[array.Count];
                for (int i = 0; i < array.Count; i++)
                {
                    IJsonValue item = array[i];
                    if (item != null && item.ValueType != JsonValueType.Null)
                    {
                        JsonObject obj = item.GetObject();
                        result[i] = new ComplexType
                        {
                            Name = obj["Name"].GetString(),
                            Age  = (int)obj["Age"].GetNumber()
                        };
                    }
                }

                return(result);
            }
        }
Beispiel #3
0
        internal static object ValueToObject(IJsonValue value)
        {
            switch (value.ValueType)
            {
            case JsonValueType.Null:
                return(null);

            case JsonValueType.Boolean:
                return(value.GetBoolean());

            case JsonValueType.Number:
                return(value.GetNumber());

            case JsonValueType.String:
                return(value.GetString());

            case JsonValueType.Object:
                return(new JsonPropertyProvider(value.GetObject()));

            case JsonValueType.Array:
                return(new JsonVectorView(value.GetArray()));

            default:
                throw new ArgumentException();
            }
        }
Beispiel #4
0
 dynamic Convert(IJsonValue json)
 {
     dynamic obj = null;
     switch (json.ValueType)
     {
         case JsonValueType.Array:
             obj = ConvertArray(json.GetArray());
             break;
         case JsonValueType.Boolean:
             obj = json.GetBoolean();
             break;
         case JsonValueType.Null:
             obj = null;
             break;
         case JsonValueType.Number:
             obj = json.GetNumber();
             break;
         case JsonValueType.Object:
             obj = new JsonToDynamic(json.GetObject());
             break;
         case JsonValueType.String:
             obj = json.GetString();
             break;
     }
     return obj;
 }
        public static object[] ToObjectArray(this IJsonValue v)
        {
            List <object> result = new List <object>();

            switch (v.ValueType)
            {
            case JsonValueType.Boolean:
                result.Add(v.GetBoolean());
                break;

            case JsonValueType.Null:
                result.Add(null);
                break;

            case JsonValueType.Number:
                result.Add(v.GetNumber());
                break;

            case JsonValueType.String:
                result.Add(v.GetString());
                break;

            case JsonValueType.Array:
                result.Add(v.GetArray().ToObjectArray());
                break;
            }

            return(result.ToArray());
        }
 private void WriteJsonValue(JsonWriter writer, IJsonValue value)
 {
     switch (value.ValueType)
     {
         case JsonValueType.Array:
         {
             JsonArray a = value.GetArray();
             writer.WriteStartArray();
             for (int i = 0; i < a.Count; i++)
             {
                 WriteJsonValue(writer, a[i]);
             }
             writer.WriteEndArray();
         }
             break;
         case JsonValueType.Boolean:
         {
             writer.WriteValue(value.GetBoolean());
         }
             break;
         case JsonValueType.Null:
         {
             writer.WriteNull();
         }
             break;
         case JsonValueType.Number:
         {
             // JsonValue doesn't support integers
             // serialize whole numbers without a decimal point
             double d = value.GetNumber();
             bool isInteger = (d % 1 == 0);
             if (isInteger && d <= long.MaxValue && d >= long.MinValue)
                 writer.WriteValue(Convert.ToInt64(d));
             else
                 writer.WriteValue(d);
         }
             break;
         case JsonValueType.Object:
         {
             JsonObject o = value.GetObject();
             writer.WriteStartObject();
             foreach (KeyValuePair<string, IJsonValue> v in o)
             {
                 writer.WritePropertyName(v.Key);
                 WriteJsonValue(writer, v.Value);
             }
             writer.WriteEndObject();
         }
             break;
         case JsonValueType.String:
         {
             writer.WriteValue(value.GetString());
         }
             break;
         default:
             throw new ArgumentOutOfRangeException("ValueType");
     }
 }
        private void WriteJsonValue(JsonWriter writer, IJsonValue value)
        {
            switch (value.ValueType)
            {
            case JsonValueType.Array:
            {
                JsonArray a = value.GetArray();
                writer.WriteStartArray();
                for (int i = 0; i < a.Count; i++)
                {
                    WriteJsonValue(writer, a[i]);
                }
                writer.WriteEndArray();
            }
            break;

            case JsonValueType.Boolean:
            {
                writer.WriteValue(value.GetBoolean());
            }
            break;

            case JsonValueType.Null:
            {
                writer.WriteNull();
            }
            break;

            case JsonValueType.Number:
            {
                writer.WriteValue(value.GetNumber());
            }
            break;

            case JsonValueType.Object:
            {
                JsonObject o = value.GetObject();
                writer.WriteStartObject();
                foreach (KeyValuePair <string, IJsonValue> v in o)
                {
                    writer.WritePropertyName(v.Key);
                    WriteJsonValue(writer, v.Value);
                }
                writer.WriteEndObject();
            }
            break;

            case JsonValueType.String:
            {
                writer.WriteValue(value.GetString());
            }
            break;

            default:
                throw new ArgumentOutOfRangeException("ValueType");
            }
        }
        public static async Task <Analyzer> Order(string sentence)
        {
            Analyzer result = new Analyzer
            {
                Entity = Entity.AllLights,
                Intent = Intent.None
            };

            try
            {
                string queryUrl   = LuisUrl + sentence;
                string resultJson = await Get(queryUrl);

                //convert JSON in comands
                JsonObject obj    = JsonObject.Parse(resultJson);
                string     intent = obj["topScoringIntent"].GetObject()["intent"].GetString();
                switch (intent)
                {
                case "LightOn":
                    result.Intent = Intent.LightOn;
                    break;

                case "LightOff":
                    result.Intent = Intent.LightOff;
                    break;
                }

                IJsonValue entity = obj["entities"];
                if (entity.GetArray().Any())
                {
                    string entityLight = obj["entities"].GetArray()[0].GetObject()["type"].ToString().Replace("\"", "");
                    switch (entityLight)
                    {
                    case "AllLight":
                        result.Entity = Entity.AllLights;
                        break;

                    case "Green":
                        result.Entity = Entity.Green;
                        break;

                    case "Yellow":
                        result.Entity = Entity.Yellow;
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception)
            {
                //TODO
            }

            return(result);
        }
 internal AnimatablePathValue(IJsonValue json, LottieComposition composition)
 {
     if (HasKeyframes(json))
     {
         var jsonArray = json.GetArray();
         var length    = jsonArray.Count;
         for (var i = 0; i < length; i++)
         {
             var jsonKeyframe = jsonArray[i].GetObject();
             var keyframe     = PathKeyframe.PathKeyframeFactory.NewInstance(jsonKeyframe, composition, ValueFactory.Instance);
             _keyframes.Add(keyframe);
         }
         Keyframe <PathKeyframe> .SetEndFrames <PathKeyframe, PointF>(_keyframes);
     }
     else
     {
         _initialPoint = JsonUtils.PointFromJsonArray(json.GetArray(), composition.DpScale);
     }
 }
Beispiel #10
0
        private static object _jsonDeserialize_convert(IJsonValue json)
        {
            object obj = null;

            switch (json.ValueType)
            {
            case JsonValueType.Array:
                JsonArray jsonArray = json.GetArray();
                object[]  objArray  = new object[jsonArray.Count];
                for (int i1 = 0; i1 < jsonArray.Count; i1++)
                {
                    objArray[i1] = _jsonDeserialize_convert(jsonArray[i1]);
                }
                obj = objArray;
                break;

            case JsonValueType.Boolean:
                obj = json.GetBoolean();
                break;

            case JsonValueType.Null:
                obj = null;
                break;

            case JsonValueType.Number:
                obj = json.GetNumber();
                break;

            case JsonValueType.Object:
                JsonObject jsonObject = json.GetObject();

                Dictionary <string, object> d = new Dictionary <string, object>();

                List <string> keys = new List <string>();
                foreach (var key in jsonObject.Keys)
                {
                    keys.Add(key);
                }

                int i2 = 0;
                foreach (var item in jsonObject.Values)
                {
                    d.Add(keys[i2], _jsonDeserialize_convert(item));
                    i2++;
                }
                obj = d;
                break;

            case JsonValueType.String:
                obj = json.GetString();
                break;
            }
            return(obj);
        }
        private static bool HasKeyframes(IJsonValue json)
        {
            if (json.ValueType != JsonValueType.Array)
            {
                return(false);
            }

            var firstObject = json.GetArray()[0];

            return(firstObject.ValueType == JsonValueType.Object && firstObject.GetObject().ContainsKey("t"));
        }
Beispiel #12
0
 internal static float ValueFromObject(IJsonValue @object)
 {
     if (@object.ValueType == JsonValueType.Number)
     {
         return((float)@object.GetNumber());
     }
     if (@object.ValueType == JsonValueType.Array)
     {
         return((float)@object.GetArray()[0].GetNumber());
     }
     return(0);
 }
Beispiel #13
0
        static private object ConvertStruct(ITypeDefinition type, IJsonValue val)
        {
            AllJoynMessageArgStructure @struct = new AllJoynMessageArgStructure(type);

            IEnumerable <object> fields = Enumerable.Zip(type.Fields, val.GetArray(), Convert);

            foreach (object elem in fields)
            {
                @struct.Add(elem);
            }

            return(@struct);
        }
        public PointF ValueFromObject(IJsonValue @object, float scale)
        {
            if (@object.ValueType == JsonValueType.Array)
            {
                return(JsonUtils.PointFromJsonArray(@object.GetArray(), scale));
            }
            if (@object.ValueType == JsonValueType.Object)
            {
                return(JsonUtils.PointFromJsonObject(@object.GetObject(), scale));
            }

            throw new System.ArgumentException("Unable to parse point from " + @object);
        }
 private void WriteJsonValue(JsonWriter writer, IJsonValue value)
 {
   switch (value.ValueType)
   {
     case JsonValueType.Array:
       {
         JsonArray a = value.GetArray();
         writer.WriteStartArray();
         for (int i = 0; i < a.Count; i++)
         {
           WriteJsonValue(writer, a[i]);
         }
         writer.WriteEndArray();
       }
       break;
     case JsonValueType.Boolean:
       {
         writer.WriteValue(value.GetBoolean());
       }
       break;
     case JsonValueType.Null:
       {
         writer.WriteNull();
       }
       break;
     case JsonValueType.Number:
       {
         writer.WriteValue(value.GetNumber());
       }
       break;
     case JsonValueType.Object:
       {
         JsonObject o = value.GetObject();
         writer.WriteStartObject();
         foreach (KeyValuePair<string, IJsonValue> v in o)
         {
           writer.WritePropertyName(v.Key);
           WriteJsonValue(writer, v.Value);
         }
         writer.WriteEndObject();
       }
       break;
     case JsonValueType.String:
       {
         writer.WriteValue(value.GetString());
       }
       break;
     default:
       throw new ArgumentOutOfRangeException("ValueType");
   }
 }
Beispiel #16
0
        private static void PrettyPrintValue(StringBuilder builder, IJsonValue jsonValue, int indent)
        {
            switch (jsonValue.ValueType)
            {
            case JsonValueType.String:
                builder.Append($"\"{jsonValue.GetString()}\"");
                break;

            case JsonValueType.Boolean:
                builder.Append($"{jsonValue.GetBoolean()}");
                break;

            case JsonValueType.Number:
                builder.Append($"{jsonValue.GetNumber()}");
                break;

            case JsonValueType.Null:
                builder.Append($"null");
                break;

            case JsonValueType.Object:
                builder.Append("{\r\n");
                indent++;
                foreach (var item in jsonValue.GetObject())    //doesn't preserve order
                {
                    Tabs(builder, indent);
                    builder.Append($"\"{item.Key}\": ");
                    PrettyPrintValue(builder, (item.Value), indent);
                }
                RemoveLastComma(builder);
                indent--;
                Tabs(builder, indent);
                builder.Append("}");
                break;

            case JsonValueType.Array:
                builder.Append("[\r\n");
                indent++;
                foreach (IJsonValue value in jsonValue.GetArray())
                {
                    Tabs(builder, indent);
                    PrettyPrintValue(builder, value, indent);
                }
                RemoveLastComma(builder);
                indent--;
                Tabs(builder, indent);
                builder.Append("]");
                break;
            }
            builder.Append(",\r\n");
        }
Beispiel #17
0
        public static IJsonValue Get(this IJsonValue d, int s)
        {
            if (d.ValueType != JsonValueType.Array)
            {
                return(null);
            }
            var i = d.GetArray();

            if (i.Count > s || s < 0)
            {
                return(null);
            }
            return(i[s]);
        }
        private static object _jsonDeserialize_convert(IJsonValue json)
        {
            object obj = null;
            switch (json.ValueType)
            {
                case JsonValueType.Array:
                    JsonArray jsonArray = json.GetArray();
                    object[] objArray = new object[jsonArray.Count];
                    for (int i1 = 0; i1 < jsonArray.Count; i1++)
                    {
                        objArray[i1] = _jsonDeserialize_convert(jsonArray[i1]);
                    }
                    obj = objArray;
                    break;
                case JsonValueType.Boolean:
                    obj = json.GetBoolean();
                    break;
                case JsonValueType.Null:
                    obj = null;
                    break;
                case JsonValueType.Number:
                    obj = json.GetNumber();
                    break;
                case JsonValueType.Object:
                    JsonObject jsonObject = json.GetObject();

                    Dictionary<string, object> d = new Dictionary<string, object>();

                    List<string> keys = new List<string>();
                    foreach (var key in jsonObject.Keys)
                    {
                        keys.Add(key);
                    }

                    int i2 = 0;
                    foreach (var item in jsonObject.Values)
                    {
                        d.Add(keys[i2], _jsonDeserialize_convert(item));
                        i2++;
                    }
                    obj = d;
                    break;
                case JsonValueType.String:
                    obj = json.GetString();
                    break;
            }
            return obj;
        }
            /// <summary>
            /// Both the color stops and opacity stops are in the same array.
            /// There are #colorPoints colors sequentially as:
            /// [
            ///     ...,
            ///     position,
            ///     red,
            ///     green,
            ///     blue,
            ///     ...
            /// ]
            ///
            /// The remainder of the array is the opacity stops sequentially as:
            /// [
            ///     ...,
            ///     position,
            ///     opacity,
            ///     ...
            /// ]
            /// </summary>
            public GradientColor ValueFromObject(IJsonValue @object, float scale)
            {
                var array         = @object.GetArray();
                var positions     = new float[_colorPoints];
                var colors        = new Color[_colorPoints];
                var gradientColor = new GradientColor(positions, colors);
                var r             = 0;
                var g             = 0;

                if (array.Count != _colorPoints * 4)
                {
                    Debug.WriteLine("Unexpected gradient length: " + array.Count + ". Expected " + _colorPoints * 4 + ". This may affect the appearance of the gradient. " + "Make sure to save your After Effects file before exporting an animation with " + "gradients.", LottieLog.Tag);
                }
                for (var i = 0; i < _colorPoints * 4; i++)
                {
                    var colorIndex = i / 4;
                    var value      = array[i].GetNumber();
                    switch (i % 4)
                    {
                    case 0:
                        // position
                        positions[colorIndex] = (float)value;
                        break;

                    case 1:
                        r = (int)(value * 255);
                        break;

                    case 2:
                        g = (int)(value * 255);
                        break;

                    case 3:
                        var b = (int)(value * 255);
                        colors[colorIndex] = Color.FromArgb(255, (byte)r, (byte)g, (byte)b);
                        break;
                    }
                }

                AddOpacityStopsToGradientIfNeeded(gradientColor, array);
                return(gradientColor);
            }
Beispiel #20
0
        private void RenderValue(IJsonValue json, double indent)
        {
            switch (json.ValueType)
            {
            case JsonValueType.Array:
                RenderArray(json.GetArray(), indent);
                break;

            case JsonValueType.Object:
                RenderObject(json.GetObject(), indent);
                break;

            case JsonValueType.Null:
                AddInlines(new Run()
                {
                    Text = "null", FontStyle = Windows.UI.Text.FontStyle.Italic, Foreground = BooleanBrush
                });
                break;

            case JsonValueType.Number:
                AddInlines(new Run()
                {
                    Text = json.GetNumber().ToString(), Foreground = NumberBrush
                });
                break;

            case JsonValueType.String:
                AddInlines(new Run()
                {
                    Text = "\"" + json.GetString() + "\"", Foreground = StringBrush
                });
                break;

            case JsonValueType.Boolean:
                AddInlines(new Run()
                {
                    Text = json.GetBoolean().ToString(), Foreground = BooleanBrush
                });
                break;
            }
        }
        public Color ValueFromObject(IJsonValue @object, float scale)
        {
            var colorArray = @object.GetArray();

            if (colorArray.Count == 4)
            {
                var shouldUse255 = true;
                for (var i = 0; i < colorArray.Count; i++)
                {
                    var colorChannel = colorArray[i].GetNumber();
                    if (colorChannel > 1f)
                    {
                        shouldUse255 = false;
                    }
                }

                var multiplier = shouldUse255 ? 255f : 1f;
                return(Color.FromArgb((byte)(colorArray[3].GetNumber() * multiplier), (byte)(colorArray[0].GetNumber() * multiplier), (byte)(colorArray[1].GetNumber() * multiplier), (byte)(colorArray[2].GetNumber() * multiplier)));
            }
            return(Colors.Black);
        }
        public void Emit(string eventName, IJsonValue args, AckCallback ackCallback)
        {
            SIO_Packet p = new SIO_Packet(SIO_Packet.PacketType.EVENT);

            p.Namespace = Name;

            if (args.ValueType == JsonValueType.Array)
            {
                args.GetArray().Insert(0, JsonValue.CreateStringValue(eventName));
                p.Data = args;
            }
            else
            {
                var data = new JsonArray();
                data.Add(JsonValue.CreateStringValue(eventName));
                data.Add(args);
                p.Data = data;
            }

            client.Send(p.Encode());
        }
Beispiel #23
0
        public static string GetValueString(this IJsonValue value)
        {
            StringBuilder builder;

            switch (value.ValueType)
            {
            case JsonValueType.String:
                return(value.GetString());

            case JsonValueType.Boolean:
                return(value.GetBoolean().ToString());

            case JsonValueType.Number:
                return(value.GetNumber().ToString());

            case JsonValueType.Array:
                builder = new StringBuilder();
                foreach (IJsonValue item in value.GetArray())
                {
                    builder.AppendLine(GetValueString(item));
                }
                return(builder.ToString());

            case JsonValueType.Object:
                builder = new StringBuilder();
                foreach (IJsonValue item in value.GetObject().Values)
                {
                    builder.AppendLine(GetValueString(item));
                }
                return(builder.ToString());

            case JsonValueType.Null:
            default:
                return(null);
            }
        }
Beispiel #24
0
        public static bool CompareJson(IJsonValue expected, IJsonValue actual, List <string> errors)
        {
            if (expected == null)
            {
                return(actual == null);
            }

            if (actual == null)
            {
                return(false);
            }

            if (expected.ValueType != actual.ValueType)
            {
                errors.Add(string.Format("Expected value type {0} != actual {1}", expected.ValueType, actual.ValueType));
                return(false);
            }

            switch (expected.ValueType)
            {
            case JsonValueType.Boolean:
                return(expected.GetBoolean() == actual.GetBoolean());

            case JsonValueType.Null:
                return(true);

            case JsonValueType.String:
                return(expected.GetString() == actual.GetString());

            case JsonValueType.Number:
                double expectedNumber    = expected.GetNumber();
                double actualNumber      = actual.GetNumber();
                double delta             = 1 - expectedNumber / actualNumber;
                double acceptableEpsilon = 0.000001;
                if (Math.Abs(delta) > acceptableEpsilon)
                {
                    errors.Add(string.Format("Numbers differ more than the allowed difference: {0} - {1}",
                                             expected.Stringify(), actual.Stringify()));
                    return(false);
                }
                else
                {
                    return(true);
                }

            case JsonValueType.Array:
                JsonArray expectedArray = expected.GetArray();
                JsonArray actualArray   = actual.GetArray();
                if (expectedArray.Count != actualArray.Count)
                {
                    errors.Add(string.Format("Size of arrays are different: expected {0} != actual {1}", expectedArray.Count, actualArray.Count));
                    return(false);
                }

                for (int i = 0; i < expectedArray.Count; i++)
                {
                    if (!CompareJson(expectedArray[i], actualArray[i], errors))
                    {
                        errors.Add("Difference in array item at index " + i);
                        return(false);
                    }
                }

                return(true);

            case JsonValueType.Object:
                JsonObject expectedObject = expected.GetObject();
                JsonObject actualObject   = actual.GetObject();
                foreach (var key in expectedObject.Keys)
                {
                    if (key == "id")
                    {
                        continue;                  // set by server, ignored at comparison
                    }
                    if (!actualObject.ContainsKey(key))
                    {
                        errors.Add(string.Format("Expected object contains a pair with key {0}, actual does not.", key));
                        return(false);
                    }

                    if (!CompareJson(expectedObject[key], actualObject[key], errors))
                    {
                        errors.Add("Difference in object member with key " + key);
                        return(false);
                    }
                }

                return(true);

            default:
                throw new ArgumentException("Don't know how to compare IJsonValue of type " + expected.ValueType);
            }
        }
Beispiel #25
0
 private void RenderValue(IJsonValue json, double indent)
 {
     switch (json.ValueType)
     {
         case JsonValueType.Array:
             RenderArray(json.GetArray(), indent);
             break;
         case JsonValueType.Object:
             RenderObject(json.GetObject(), indent);
             break;
         case JsonValueType.Null:
             AddInlines(new Run() { Text = "null", FontStyle = Windows.UI.Text.FontStyle.Italic, Foreground = BooleanBrush });
             break;
         case JsonValueType.Number:
             AddInlines(new Run() { Text = json.GetNumber().ToString(), Foreground = NumberBrush });
             break;
         case JsonValueType.String:
             AddInlines(new Run() { Text = "\"" + json.GetString() + "\"", Foreground = StringBrush });
             break;
         case JsonValueType.Boolean:
             AddInlines(new Run() { Text = json.GetBoolean().ToString(), Foreground = BooleanBrush });
             break;
     }
 }
Beispiel #26
0
            public ScaleXy ValueFromObject(IJsonValue @object, float scale)
            {
                var array = @object.GetArray();

                return(new ScaleXy((float)array.GetNumberAt(0, 1) / 100f * scale, (float)array.GetNumberAt(1, 1) / 100f * scale));
            }
Beispiel #27
0
        private void WriteJsonValue(JsonWriter writer, IJsonValue value)
        {
            switch (value.ValueType)
            {
            case JsonValueType.Array:
            {
                JsonArray a = value.GetArray();
                writer.WriteStartArray();
                for (int i = 0; i < a.Count; i++)
                {
                    WriteJsonValue(writer, a[i]);
                }
                writer.WriteEndArray();
            }
            break;

            case JsonValueType.Boolean:
            {
                writer.WriteValue(value.GetBoolean());
            }
            break;

            case JsonValueType.Null:
            {
                writer.WriteNull();
            }
            break;

            case JsonValueType.Number:
            {
                // JsonValue doesn't support integers
                // serialize whole numbers without a decimal point
                double d         = value.GetNumber();
                bool   isInteger = (d % 1 == 0);
                if (isInteger && d <= long.MaxValue && d >= long.MinValue)
                {
                    writer.WriteValue(Convert.ToInt64(d));
                }
                else
                {
                    writer.WriteValue(d);
                }
            }
            break;

            case JsonValueType.Object:
            {
                JsonObject o = value.GetObject();
                writer.WriteStartObject();
                foreach (KeyValuePair <string, IJsonValue> v in o)
                {
                    writer.WritePropertyName(v.Key);
                    WriteJsonValue(writer, v.Value);
                }
                writer.WriteEndObject();
            }
            break;

            case JsonValueType.String:
            {
                writer.WriteValue(value.GetString());
            }
            break;

            default:
                throw new ArgumentOutOfRangeException("ValueType");
            }
        }
 public static JsonArray AsArray(this IJsonValue value)
 {
     return((value != null && value.ValueType == JsonValueType.Array) ?
            value.GetArray() :
            null);
 }
Beispiel #29
0
            public ShapeData ValueFromObject(IJsonValue @object, float scale)
            {
                JsonObject pointsData = null;

                if (@object.ValueType == JsonValueType.Array)
                {
                    var firstObject = @object.GetArray()[0];
                    if (firstObject.ValueType == JsonValueType.Object && firstObject.GetObject().ContainsKey("v"))
                    {
                        pointsData = firstObject.GetObject();
                    }
                }
                else if (@object.ValueType == JsonValueType.Object && @object.GetObject().ContainsKey("v"))
                {
                    pointsData = @object.GetObject();
                }

                if (pointsData == null)
                {
                    return(null);
                }

                var pointsArray = pointsData.GetNamedArray("v", null);
                var inTangents  = pointsData.GetNamedArray("i", null);
                var outTangents = pointsData.GetNamedArray("o", null);
                var closed      = pointsData.GetNamedBoolean("c", false);

                if (pointsArray == null || inTangents == null || outTangents == null || pointsArray.Count != inTangents.Count || pointsArray.Count != outTangents.Count)
                {
                    throw new System.InvalidOperationException("Unable to process points array or tangents. " + pointsData);
                }
                if (pointsArray.Count == 0)
                {
                    return(new ShapeData(new PointF(), false, new List <CubicCurveData>()));
                }

                var length = pointsArray.Count;
                var vertex = VertexAtIndex(0, pointsArray);

                vertex.X *= scale;
                vertex.Y *= scale;
                var initialPoint             = vertex;
                List <CubicCurveData> curves = new List <CubicCurveData>(length);

                for (var i = 1; i < length; i++)
                {
                    vertex = VertexAtIndex(i, pointsArray);
                    var previousVertex = VertexAtIndex(i - 1, pointsArray);
                    var cp1            = VertexAtIndex(i - 1, outTangents);
                    var cp2            = VertexAtIndex(i, inTangents);
                    var shapeCp1       = MiscUtils.AddPoints(previousVertex, cp1);
                    var shapeCp2       = MiscUtils.AddPoints(vertex, cp2);

                    shapeCp1.X *= scale;
                    shapeCp1.Y *= scale;
                    shapeCp2.X *= scale;
                    shapeCp2.Y *= scale;
                    vertex.X   *= scale;
                    vertex.Y   *= scale;

                    curves.Add(new CubicCurveData(shapeCp1, shapeCp2, vertex));
                }

                if (closed)
                {
                    vertex = VertexAtIndex(0, pointsArray);
                    var previousVertex = VertexAtIndex(length - 1, pointsArray);
                    var cp1            = VertexAtIndex(length - 1, outTangents);
                    var cp2            = VertexAtIndex(0, inTangents);

                    var shapeCp1 = MiscUtils.AddPoints(previousVertex, cp1);
                    var shapeCp2 = MiscUtils.AddPoints(vertex, cp2);

                    if (scale != 1f)
                    {
                        shapeCp1.X *= scale;
                        shapeCp1.Y *= scale;
                        shapeCp2.X *= scale;
                        shapeCp2.Y *= scale;
                        vertex.X   *= scale;
                        vertex.Y   *= scale;
                    }

                    curves.Add(new CubicCurveData(shapeCp1, shapeCp2, vertex));
                }
                return(new ShapeData(initialPoint, closed, curves));
            }
Beispiel #30
0
        public bool LoadMonthMenu(int Year, int Month, DeviceType deviceType)
        {
            var Connection = CheckNetwork(deviceType);

            if (Connection == NetworkType.None || Connection == NetworkType.Error)
            {
                return(false);
            }
            var JObject = RequestMonthMenu(Year, Month);

            if (JObject == null)
            {
                return(false);
            }

            foreach (var JItem_Temp1 in JObject)
            {
                var JItem_Date = JItem_Temp1.GetObject();
                if (JItem_Date.Keys.Count != 4)
                {
                    continue;
                }
                foreach (var JItem_Temp2 in JItem_Date)
                {
                    switch (JItem_Temp2.Key)
                    {
                    case "date":
                        JItem_Day = JItem_Temp2.Value;
                        break;

                    case "breakfast":
                        JItem_Breakfast = JItem_Temp2.Value;
                        break;

                    case "lunch":
                        JItem_Lunch = JItem_Temp2.Value;
                        break;

                    case "dinner":
                        JItem_Dinner = JItem_Temp2.Value;
                        break;
                    }
                }

                if (JItem_Day.ValueType != JsonValueType.String)
                {
                    continue;
                }
                bool IsDay = int.TryParse(JItem_Day.GetString(), out var Day);
                if (IsDay == false)
                {
                    continue;
                }

                string Breakfast_String = "";
                string Lunch_String     = "";
                string Dinner_String    = "";

                if (JItem_Breakfast.ValueType != JsonValueType.Array)
                {
                    Breakfast_String = "급식 정보 없음";
                }
                else
                {
                    var Breakfast_Array = JItem_Breakfast.GetArray();
                    for (int i = 0; i < Breakfast_Array.Count; i++)
                    {
                        string Breakfast_TempString = Breakfast_Array[i].GetString();
                        Breakfast_String += RemoveAllergyInfo(Breakfast_TempString);
                        Breakfast_String += "\n";
                    }
                }

                if (JItem_Lunch.ValueType != JsonValueType.Array)
                {
                    Lunch_String = "급식 정보 없음";
                }
                else
                {
                    var Lunch_Array = JItem_Lunch.GetArray();
                    for (int j = 0; j < Lunch_Array.Count; j++)
                    {
                        string Lunch_TempString = Lunch_Array[j].GetString();
                        Lunch_String += RemoveAllergyInfo(Lunch_TempString);
                        Lunch_String += "\n";
                    }
                }

                if (JItem_Dinner.ValueType != JsonValueType.Array)
                {
                    Dinner_String = "급식 정보 없음";
                }
                else
                {
                    var Dinner_Array = JItem_Dinner.GetArray();
                    for (int k = 0; k < Dinner_Array.Count; k++)
                    {
                        string Dinner_TempString = Dinner_Array[k].GetString();
                        Dinner_String += RemoveAllergyInfo(Dinner_TempString);
                        Dinner_String += "\n";
                    }
                }

                switch (deviceType)
                {
                case DeviceType.Win10:
                    ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
                    localSettings.Values[MakeDateString(Year, Month, Day) + "B"] = Breakfast_String;
                    localSettings.Values[MakeDateString(Year, Month, Day) + "L"] = Lunch_String;
                    localSettings.Values[MakeDateString(Year, Month, Day) + "D"] = Dinner_String;
                    break;

                case DeviceType.Win7:

                    break;

                case DeviceType.XF:
                    Preferences.Set(MakeDateString(Year, Month, Day) + "B", Breakfast_String);
                    Preferences.Set(MakeDateString(Year, Month, Day) + "L", Lunch_String);
                    Preferences.Set(MakeDateString(Year, Month, Day) + "D", Dinner_String);
                    break;

                default:
                    break;
                }
            }
            return(true);
        }
Beispiel #31
0
        private bool JsonComparesEqual(IJsonValue expected, IJsonValue actual)
        {
            if (expected.ValueType != actual.ValueType)
            {
                return(false);
            }
            switch (expected.ValueType)
            {
            case JsonValueType.String:
                if (expected.GetString() == actual.GetString())
                {
                    return(true);
                }
                else
                {
                    Debug.WriteLine($"Expected {expected.GetString()} got {actual.GetString()}"); return(false);
                }

            case JsonValueType.Number:
                return(expected.GetNumber() == actual.GetNumber());

            case JsonValueType.Boolean:
                return(expected.GetBoolean() == actual.GetBoolean());

            case JsonValueType.Null:
                return(true);

            case JsonValueType.Array:
            {
                var ea = expected.GetArray();
                var aa = actual.GetArray();
                if (ea.Count != aa.Count)
                {
                    return(false);
                }
                for (uint i = 0; i < ea.Count; i++)
                {
                    if (!JsonComparesEqual(ea[(int)i], aa[(int)i]))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            case JsonValueType.Object:
            {
                var eo = expected.GetObject();
                var ao = actual.GetObject();
                if (eo.Keys.Count != ao.Keys.Count)
                {
                    return(false);
                }
                foreach (var key in eo.Keys)
                {
                    if (!JsonComparesEqual(eo.GetNamedValue(key), ao.GetNamedValue(key)))
                    {
                        return(false);
                    }
                }
                return(true);
            }

            default:
                throw new ArgumentException();
            }
        }
Beispiel #32
0
        private static bool JsonComparesEqual(IJsonValue expected, IJsonValue actual, string keyName)
        {
            const string Anything = "<ANYTHING>";

            if (expected.ValueType == JsonValueType.String && Regex.Replace(expected.GetString(), @"\p{C}", "") == Anything)
            {
                Debug.WriteLine($"Skipping ignored value: {actual.ValueType} {actual}");
                return(true);
            }
            //Debug.WriteLine($"keyname: {keyName} {expected.ValueType} {actual.ValueType}");
            if (expected.ValueType != actual.ValueType)
            {
                Debug.WriteLine($"Expected {expected} got {actual}");
                return(false);
            }
            switch (expected.ValueType)
            {
            case JsonValueType.String:
                if (expected.GetString() != actual.GetString())
                {
                    Debug.WriteLine($"string:Expected {expected.GetString()} got {actual.GetString()}");
                    return(false);
                }
                return(true);

            case JsonValueType.Number:
                if (Math.Abs(expected.GetNumber() - actual.GetNumber()) > epsilon)
                {
                    Debug.WriteLine($"number: {keyName} {expected.GetNumber()} {actual.GetNumber()}");
                    return(false);
                }
                return(true);

            case JsonValueType.Boolean:
                return(expected.GetBoolean() == actual.GetBoolean());

            case JsonValueType.Null:
                return(true);

            case JsonValueType.Array:
            {
                var ea = expected.GetArray();
                var aa = actual.GetArray();
                if (!JsonCompareArray(ea, aa))
                {
                    Debug.WriteLine("in key " + keyName);
                    return(false);
                }
                return(true);
            }

            case JsonValueType.Object:
            {
                if (!JsonCompareObject(expected.GetObject(), actual.GetObject()))
                {
                    Debug.WriteLine("in key " + keyName);
                    return(false);
                }
                return(true);
            }

            default:
                throw new ArgumentException();
            }
        }
Beispiel #33
0
        static public object Convert(ITypeDefinition type, IJsonValue val)
        {
            try
            {
                switch (type.Type)
                {
                case TypeId.Boolean:
                    return(val.GetBoolean());

                case TypeId.Double:
                    return(val.GetNumber());

                case TypeId.Dictionary:
                    return(val.GetArray()
                           .ToList()
                           .Select(x =>
                                   new KeyValuePair <object, object>(
                                       Convert(type.KeyType, x.GetArray()[0]),
                                       Convert(type.ValueType, x.GetArray()[1])
                                       )
                                   ));

                case TypeId.Signature:
                    return(val.GetString());

                case TypeId.Int32:
                    return((Int32)val.GetNumber());

                case TypeId.Int16:
                    return((Int16)val.GetNumber());

                case TypeId.ObjectPath:
                    return(val.GetString());

                case TypeId.Uint16:
                    return((UInt16)val.GetNumber());

                case TypeId.Struct:
                    return(ConvertStruct(type, val));

                case TypeId.String:
                    return(val.GetString());

                case TypeId.Uint64:
                    return((UInt64)val.GetNumber());

                case TypeId.Uint32:
                    return((UInt32)val.GetNumber());

                case TypeId.Variant:
                    return(ConvertVariant(val));

                case TypeId.Int64:
                    return((Int64)val.GetNumber());

                case TypeId.Uint8:
                    return((byte)val.GetNumber());

                case TypeId.BooleanArray:
                    return(val.GetArray().Select(x => x.GetBoolean()).Cast <object>().ToList());

                case TypeId.DoubleArray:
                    return(val.GetArray().Select(x => x.GetNumber()).Cast <object>().ToList());

                case TypeId.Int32Array:
                    return(val.GetArray().Select(x => (Int32)x.GetNumber()).Cast <object>().ToList());

                case TypeId.Int16Array:
                    return(val.GetArray().Select(x => (Int16)x.GetNumber()).Cast <object>().ToList());

                case TypeId.Uint16Array:
                    return(val.GetArray().Select(x => (UInt16)x.GetNumber()).Cast <object>().ToList());

                case TypeId.Uint64Array:
                    return(val.GetArray().Select(x => (UInt64)x.GetNumber()).Cast <object>().ToList());

                case TypeId.Uint32Array:
                    return(val.GetArray().Select(x => (UInt32)x.GetNumber()).Cast <object>().ToList());

                case TypeId.VariantArray:
                    return(val.GetArray().Select(ConvertVariant).Cast <object>().ToList());

                case TypeId.Int64Array:
                    return(val.GetArray().Select(x => (Int64)x.GetNumber()).Cast <object>().ToList());

                case TypeId.Uint8Array:
                    return(val.GetArray().Select(x => (byte)x.GetNumber()).Cast <object>().ToList());

                case TypeId.SignatureArray:
                    return(val.GetArray().Select(x => x.GetString()).Cast <object>().ToList());

                case TypeId.ObjectPathArray:
                    return(val.GetArray().Select(x => x.GetString()).Cast <object>().ToList());

                case TypeId.StringArray:
                    return(val.GetArray().Select(x => x.GetString()).Cast <object>().ToList());

                case TypeId.StructArray:
                    return(val.GetArray().Select(x => ConvertStruct(type, x)).Cast <object>().ToList());
                }
            }
            catch (Exception) { };

            throw Logger.LogException("AllJoyn cast", new InvalidCastException());
        }
 public PointF ValueFromObject(IJsonValue @object, float scale)
 {
     return(JsonUtils.PointFromJsonArray(@object.GetArray(), scale));
 }