Example #1
0
 public void WriteJson(object value, IValueWriter writer)
 {
     if (value is null)
     {
         writer.Null();
     }
     else
     {
         writer.Long(((UnixMillisecondTime)value).Value);
     }
 }
            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()));
                }
            }