You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We might need to expose some APIs to provide a way to (de-)serialize State. With Json.NET, it doesn't actually need any changes to the library, here's a quick and dirty implementation of a JsonConverter:
classStateJsonConverter:JsonConverter{publicoverrideboolCanConvert(TypeobjectType){if(objectType.IsGenericType){// field type definition for deserialization - the interface directlyif(objectType.GetGenericTypeDefinition()==typeof(State<>)){returntrue;}// run-time object type for serialization - the implementationforeach(varifaceinobjectType.GetInterfaces()){if(iface.IsGenericType&&iface.GetGenericTypeDefinition()==typeof(State<>)){returntrue;}}}returnfalse;}publicoverridevoidWriteJson(JsonWriterwriter,objectvalue,JsonSerializerserializer){GetConverter(value.GetType()).WriteJson(writer,value,serializer);}publicoverrideobjectReadJson(JsonReaderreader,TypeobjectType,objectexistingValue,JsonSerializerserializer){returnGetConverter(objectType).ReadJson(reader,existingValue,serializer);}StateConverterGetConverter(TypestateType){// TODO: cache convertersvarrt=typeof(StateConverter<>).MakeGenericType(stateType.GetGenericArguments()[0]);return(StateConverter)Activator.CreateInstance(rt,false);}}abstractclassStateConverter{publicabstractvoidWriteJson(JsonWriterwriter,objectvalue,JsonSerializerserializer);publicabstractobjectReadJson(JsonReaderreader,objectexistingValue,JsonSerializerserializer);}classStateConverter<T>:StateConverter{publicoverridevoidWriteJson(JsonWriterwriter,objectvalue,JsonSerializerserializer){WriteJsonInternal(writer,(State<T>)value,serializer);}publicoverrideobjectReadJson(JsonReaderreader,objectexistingValue,JsonSerializerserializer){returnReadJsonInternal(reader,existingValue,serializer);}voidWriteJsonInternal(JsonWriterwriter,State<T>state,JsonSerializerserializer){serializer.Serialize(writer,state.Value);}State<T>ReadJsonInternal(JsonReaderreader,objectexistingValue,JsonSerializerserializer){varstateValue=serializer.Deserialize<T>(reader);if(existingValueisState<T>existingState){existingState.Value=stateValue;returnexistingState;}returnObservable.State(stateValue);}}
The text was updated successfully, but these errors were encountered:
We might need to expose some APIs to provide a way to (de-)serialize State. With Json.NET, it doesn't actually need any changes to the library, here's a quick and dirty implementation of a JsonConverter:
The text was updated successfully, but these errors were encountered: