/// <summary> /// Builds the view model. /// </summary> /// <param name="state">The state.</param> /// <exception cref="Exception"></exception> public virtual void BuildViewModel(ViewModelState state) { var jsonSerializer = CreateJsonSerializer(); var viewModelConverter = new ViewModelJsonConverter(true, Mapper) { UsedSerializationMaps = new HashSet <ViewModelSerializationMap>() }; jsonSerializer.Converters.Add(viewModelConverter); var writer = new JTokenWriter(); try { jsonSerializer.Serialize(writer, state.LastSentViewModel); } catch (Exception ex) { throw new Exception( $"Could not serialize viewModel of type {state.LastSentViewModel.GetType().Name}. Serialization failed at property {writer.Path}.", ex); } writer.Token["$csrfToken"] = state.CsrfToken; var result = new JObject(); result["viewModel"] = writer.Token; result["action"] = WebSocketRequestType.SuccessfulCommand; state.ChangedViewModelJson = result; }
/// <summary> /// Serializes the view model. /// </summary> /// <param name="state">The state.</param> /// <returns></returns> public virtual string SerializeViewModel(ViewModelState state) { if (state.LastSentViewModelJson == null || state.ChangedViewModelJson["viewModel"] == null) { return(state.ChangedViewModelJson.ToString(JsonFormatting)); } state.ChangedViewModelJson?.Remove("viewModelDiff"); state.ChangedViewModelJson["viewModelDiff"] = JsonUtils.Diff( (JObject)state.LastSentViewModelJson["viewModel"], (JObject)state.ChangedViewModelJson["viewModel"], true); state.LastSentViewModelJson["viewModel"] = (JObject)state.ChangedViewModelJson["viewModel"].DeepClone(); state.ChangedViewModelJson.Remove("viewModel"); return(state.ChangedViewModelJson.ToString(JsonFormatting)); }
/// <summary> /// Populates the view model from Json sent. /// </summary> /// <param name="state">The state.</param> /// <param name="serializedPostData">The serialized post data.</param> /// <returns></returns> /// <exception cref="Exception"></exception> public virtual string PopulateViewModel(ViewModelState state, string serializedPostData) { var data = state.LastSentViewModelJson = JObject.Parse(serializedPostData); var viewModelToken = (JObject)data["viewModel"]; state.CsrfToken = viewModelToken["$csrfToken"].Value <string>(); var viewModelConverter = new ViewModelJsonConverter(true, Mapper); var serializer = CreateJsonSerializer(); serializer.Converters.Add(viewModelConverter); try { viewModelConverter.Populate(viewModelToken.CreateReader(), serializer, state.LastSentViewModel); } catch (Exception ex) { throw new Exception( $"Could not deserialize viewModel of type {state.LastSentViewModel.GetType().Name}. {GeneralViewModelRecommendations}", ex); } return((string)data["taskId"]); }
public Connection() { ViewModelState = new ViewModelState(); }