Beispiel #1
0
            public static void WriteJsonValue(LdValue value, IValueWriter writer)
            {
                switch (value.Type)
                {
                case LdValueType.Null:
                    writer.Null();
                    break;

                case LdValueType.Bool:
                    writer.Bool(value.AsBool);
                    break;

                case LdValueType.Number:
                    var asInt    = value.AsInt;
                    var asDouble = value.AsDouble;
                    if ((double)asInt == asDouble)
                    {
                        writer.Int(asInt);
                    }
                    else
                    {
                        writer.Double(asDouble);
                    }
                    break;

                case LdValueType.String:
                    writer.String(value.AsString);
                    break;

                case LdValueType.Array:
                    var arr = writer.Array();
                    foreach (var v in value.List)
                    {
                        WriteJsonValue(v, arr);
                    }
                    arr.End();
                    break;

                case LdValueType.Object:
                    var obj = writer.Object();
                    foreach (var kv in value.Dictionary)
                    {
                        WriteJsonValue(kv.Value, obj.Name(kv.Key));
                    }
                    obj.End();
                    break;
                }
            }
            public void WriteJson(object instance, IValueWriter writer)
            {
                switch (instance)
                {
                case null:
                    writer.Null();
                    break;

                case bool value:
                    writer.Bool(value);
                    break;

                case int value:
                    writer.Int(value);
                    break;

                case long value:
                    writer.Long(value);
                    break;

                case float value:
                    writer.Double(value);
                    break;

                case double value:
                    writer.Double(value);
                    break;

                case string value:
                    writer.String(value);
                    break;

                case IReadOnlyDictionary <string, object> dict:
                    var obj = writer.Object();
                    foreach (var kv in dict)
                    {
                        WriteJson(kv.Value, obj.Name(kv.Key));
                    }
                    obj.End();
                    break;

                case IReadOnlyDictionary <object, object> dict:
                    var obj1 = writer.Object();
                    foreach (var kv in dict)
                    {
                        WriteJson(kv.Value, obj1.Name(kv.Key.ToString()));
                    }
                    obj1.End();
                    break;

                case IEnumerable <object> list:
                    var arr = writer.Array();
                    foreach (var o in list)
                    {
                        WriteJson(o, arr);
                    }
                    arr.End();
                    break;

                default:
                    throw new ArgumentException(string.Format("ConvertSimpleTypes does not support type {0}",
                                                              instance.GetType()));
                }
            }