public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(Variant.FromObject(null)); } if (reader.TokenType != JsonToken.StartArray) { throw new Exception("Invalid JSON for Variant type"); } var codeValue = reader.ReadAsInt32(); Debug.Assert(codeValue != null, "codeValue != null"); var typeCode = (TypeEnum)(byte)codeValue; TypeEnum subTypeCode = TypeEnum.None; if (typeCode == TypeEnum.Array) { var subTypeCodeValue = reader.ReadAsInt32(); Debug.Assert(subTypeCodeValue != null, "subTypeCodeValue != null"); subTypeCode = (TypeEnum)(byte)subTypeCodeValue; if (subTypeCode == TypeEnum.FixedBinary) { throw new NotImplementedException("Need special handling of fixed binary values"); } } if (typeCode == TypeEnum.FixedBinary) { //var size = reader.ReadAsInt32(); throw new NotImplementedException("Need special handling of fixed binary values"); } object obj; if (typeCode == TypeEnum.Object) { var objectTypeCodeValue = reader.ReadAsInt32(); Debug.Assert(objectTypeCodeValue != null, "objectTypeCodeValue != null"); var objectTypeCode = (byte)objectTypeCodeValue; var type = KnownTypeAttribute.GetType(objectTypeCode); if (!reader.Read()) { throw new Exception("Cannot read JSON"); } obj = serializer.Deserialize(reader, type); } else if (typeCode != TypeEnum.None) { if (!reader.Read()) { throw new Exception("Cannot read JSON"); } var type = VariantHelper.GetType(typeCode, subTypeCode); obj = serializer.Deserialize(reader, type); } else { obj = Variant.Create(null); } if (!reader.Read()) { throw new Exception("Cannot read JSON"); } Trace.Assert(reader.TokenType == JsonToken.EndArray); return(Variant.FromObject(obj)); }