public override Vector2 ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, Vector2 existingValue, bool hasExistingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.StartObject) { JObject item = JObject.Load(reader); existingValue.x = item["X"].Value <float>(); existingValue.y = item["Y"].Value <float>(); } else { existingValue.x = (float)reader.ReadAsDouble().Value; existingValue.y = (float)reader.ReadAsDouble().Value; } return(existingValue); }
public override Matrix4x4 ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, Matrix4x4 existingValue, bool hasExistingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.StartObject) { JObject item = JObject.Load(reader); StringBuilder str = new StringBuilder(5); for (int iy = 0; iy < 4; ++iy) { for (int ix = 0; ix < 4; ++ix) { str.Append($"m{iy}{ix}"); JToken value; if (item.TryGetValue(str.ToString(), out value)) { existingValue[iy, ix] = value.Value <float>(); } str.Clear(); } } } else { for (int iy = 0; iy < 4; ++iy) { for (int ix = 0; ix < 4; ++ix) { existingValue[iy, ix] = (float)reader.ReadAsDouble(); } } } return(existingValue); }
public override Color ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, Color existingValue, bool hasExistingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.StartObject) { JObject item = JObject.Load(reader); existingValue.r = item["R"].Value <float>(); existingValue.g = item["G"].Value <float>(); existingValue.b = item["B"].Value <float>(); existingValue.a = item["A"].Value <float>(); } else { existingValue.r = (float)reader.ReadAsDouble().Value; existingValue.g = (float)reader.ReadAsDouble().Value; existingValue.b = (float)reader.ReadAsDouble().Value; existingValue.a = (float)reader.ReadAsDouble().Value; } return(existingValue); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if( reader.TokenType == JsonToken.Null ) { return null; } if( reader.TokenType != JsonToken.StartObject ) { var msg = string.Join(" ", $"The JSON representation of a DateTime/DateTimeOffset when parsing the server response is not a {Converter.PseudoTypeKey}:{Converter.Time} object.", $"This happens if your JSON document contains DateTime/DateTimeOffsets in some other format (like an ISO8601 string) rather than a native RethinkDB pseudo type {Converter.PseudoTypeKey}:{Converter.Time} object.", $"If you are overriding the default Ser/Deserialization process, you need to make sure DateTime/DateTimeOffset are native {Converter.PseudoTypeKey}:{Converter.Time} objects before using the built-in {nameof(ReqlDateTimeConverter)}.", "See https://rethinkdb.com/docs/data-types/ for more information about how Date and Times are represented in RethinkDB."); throw new JsonSerializationException(msg); } reader.ReadAndAssertProperty(Converter.PseudoTypeKey); var reql_type = reader.ReadAsString(); if( reql_type != Converter.Time ) { throw new JsonSerializationException($"Expected {Converter.PseudoTypeKey} should be {Converter.Time} but got {reql_type}."); } reader.ReadAndAssertProperty("epoch_time"); var epoch_time = reader.ReadAsDouble(); if( epoch_time == null ) { throw new JsonSerializationException($"The {Converter.PseudoTypeKey}:{Converter.Time} object doesn't have an epoch_time value."); } reader.ReadAndAssertProperty("timezone"); var timezone = reader.ReadAsString(); //realign and get out of the pseudo type //one more post read to align out of { reql_type:TIME, .... } reader.ReadAndAssert(); if( objectType == typeof(DateTimeOffset) || objectType == typeof(DateTimeOffset?) ) { return ConvertDateTimeOffset(epoch_time.Value, timezone); } else { return ConvertDateTime(epoch_time.Value, timezone, serializer.DateTimeZoneHandling); } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { long value = (long)reader.ReadAsDouble().Value; return value.FromJsDateTime(); }