public static T DeepCopyViaTypedJson <T>(this T objectToDeepCopy) { if (objectToDeepCopy == null) { return(objectToDeepCopy); } var json = TypedJsonHelper.NewTypedJsonWriter().Write(objectToDeepCopy); return(TypedJsonHelper.NewTypedJsonReader().Read <T>(json)); }
public void TestEnumSetAndGet1() { var jsonReader = TypedJsonHelper.NewTypedJsonReader(); var jsonWriter = TypedJsonHelper.NewTypedJsonWriter(); var x1 = new ValueWrapper() { value = MyEnum1.state2 }; var json = jsonWriter.Write(x1); Log.d("json=" + json); var x2 = jsonReader.Read <ValueWrapper>(json); Assert.Equal(MyEnum1.state2, x2.GetValueAs <MyEnum1>()); }
public void TestJsonDeserializeWithImmutableList2() { var w = TypedJsonHelper.NewTypedJsonWriter(); var r = TypedJsonHelper.NewTypedJsonReader(); ServerOutbox b1 = new ServerOutbox(); b1.serverActions = ImmutableList <ServerAction> .Empty.Add(new TestAction() { myString1 = "abc" }); var json = w.Write(b1); Log.MethodEntered("TestJsonDeserializeWithImmutableList2", json); var b2 = r.Read <ServerOutbox>(json); Assert.IsType <TestAction>(b2.serverActions.First()); Assert.Equal((b1.serverActions.First() as TestAction).myString1, (b2.serverActions.First() as TestAction).myString1); }
public void TestWithTypedJson() { // Typed json includes the C# assembly types in the json, so works only in a C# only scenario to parse the // json string back into the correct C# class MySubClass1 x1 = new MySubClass1() { myString = "I am s1", myComplexField2 = new MySubClass1() { myString = "A2" } }; string json = TypedJsonHelper.NewTypedJsonWriter().Write(x1); object x2 = TypedJsonHelper.NewTypedJsonReader().Read <object>(json); Assert.True(x2 is MySubClass1); var x3 = x2 as MySubClass1; Assert.Equal(x3.myString, x1.myString); Assert.Equal((x3.myComplexField2 as MySubClass1).myString, (x1.myComplexField2 as MySubClass1).myString); }
public async Task ExampleUsage1() { var t = Log.MethodEntered("DataStoreExample3.ExampleUsage1"); // Add a thunk middleware to allow dispatching async actions: var thunkMiddleware = Middlewares.NewThunkMiddleware <MyAppState1>(); // aDD A logging middleware to log all dispatched actions: var loggingMiddleware = Middlewares.NewLoggingMiddleware <MyAppState1>(); var serverOutboxHandler = new ServerOutboxHandler <MyAppState1>(); // To allow undo redo on the full store wrap the main reducer with the undo reducer: var outboxReducer = serverOutboxHandler.Wrap(MyReducers1.ReduceMyAppState1); var initialState = new MyAppState1(); // the initial immutable state var store = new DataStore <MyAppState1>(outboxReducer, initialState, loggingMiddleware, thunkMiddleware); IoC.inject.SetSingleton(store); store.storeName = "Store 3"; { // Do a login which is an async server action that cant be cached optimistically and wont work offline: Func <Task> asyncLoginTask = async() => { await TaskV2.Delay(100); store.Dispatch(new ActionUserLoggedIn() { newLoggedInUser = new MyUser1("*****@*****.**") }); }; await(store.Dispatch(asyncLoginTask) as Task); } { // Change the email a first time: var a = new ActionOnUser.ChangeEmail() { targetEmail = "*****@*****.**", newEmail = "*****@*****.**" }; store.Dispatch(a); Assert.Equal(a, store.GetState().serverOutbox.serverActions.First()); Assert.False(store.GetState().user.emailConfirmed); } { // Change the email a second time: var a = new ActionOnUser.ChangeEmail() { targetEmail = "*****@*****.**", newEmail = "*****@*****.**" }; store.Dispatch(a); Assert.Equal(a, store.GetState().serverOutbox.serverActions.Last()); } Assert.Equal(2, store.GetState().serverOutbox.serverActions.Count); await store.SyncWithServer(store.GetState().serverOutbox.serverActions.First()); Assert.Single(store.GetState().serverOutbox.serverActions); await store.SyncWithServer(store.GetState().serverOutbox.serverActions.First()); Assert.Empty(store.GetState().serverOutbox.serverActions); Assert.True(store.GetState().user.emailConfirmed); { // Simulate a server task that has a timeout: var a = new ActionOnUser.ChangeEmail() { targetEmail = "*****@*****.**", newEmail = "*****@*****.**", simulateOneTimeout = true }; store.Dispatch(a); Assert.Single(store.GetState().serverOutbox.serverActions); Assert.False(store.GetState().user.emailConfirmed); await store.SyncWithServer(a); Assert.Empty(store.GetState().serverOutbox.serverActions); Assert.Equal(2, a.sentToServerCounter); Assert.True(store.GetState().user.emailConfirmed); } { // Simulate the server rejecting an email change: var a = new ActionOnUser.ChangeEmail() { targetEmail = "*****@*****.**", newEmail = "*****@*****.**", simulateError = true }; store.Dispatch(a); await store.SyncWithServer(a); Assert.Empty(store.GetState().serverOutbox.serverActions); Assert.Equal("*****@*****.**", store.GetState().user.email); Assert.True(store.GetState().user.emailConfirmed); } { // Test persisting and restoring the full store and continue with the pending server requests: store.Dispatch(new ActionOnUser.ChangeEmail() { targetEmail = "*****@*****.**", newEmail = "*****@*****.**" }); store.Dispatch(new ActionOnUser.ChangeEmail() { targetEmail = "*****@*****.**", newEmail = "*****@*****.**" }); Assert.Equal(2, store.GetState().serverOutbox.serverActions.Count); Assert.False(store.GetState().user.emailConfirmed); Assert.Equal("*****@*****.**", store.GetState().user.email); // Simulate persisiting the store to disk and back into memory: string persistedStateJson = TypedJsonHelper.NewTypedJsonWriter().Write(store.GetState()); store.Destroy(); // Destroy the old store before loading the state again into an new store var data2 = TypedJsonHelper.NewTypedJsonReader().Read <MyAppState1>(persistedStateJson); var store2 = new DataStore <MyAppState1>(outboxReducer, data2, loggingMiddleware, thunkMiddleware); IoC.inject.SetSingleton(store2, overrideExisting: true); store2.storeName = "Store 3 (2)"; Assert.Equal(2, store2.GetState().serverOutbox.serverActions.Count); Assert.False(store2.GetState().user.emailConfirmed); Assert.Equal("*****@*****.**", store2.GetState().user.email); // Sync the pending server tasks one after another: foreach (var serverAction in store2.GetState().serverOutbox.serverActions) { await store2.SyncWithServer(serverAction); } Assert.True(store2.GetState().user.emailConfirmed); Assert.NotNull(store2.GetState().serverOutbox); Assert.Empty(store2.GetState().serverOutbox.serverActions); } }