Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Import/Export with FromJsonString/ToJsonString #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DesktopToast/ToastManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static async Task<ToastResult> ShowAsync(string requestJson)
ToastRequest request;
try
{
request = new ToastRequest(requestJson);
request = ToastRequest.FromJsonString(requestJson);
}
catch
{
Expand Down
22 changes: 6 additions & 16 deletions DesktopToast/ToastRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
Expand Down Expand Up @@ -172,38 +171,29 @@ public string ShortcutIconFilePath
public ToastRequest()
{ }

internal ToastRequest(string requestJson) : this()
{
Import(requestJson);
}

#endregion

#region Import/Export

/// <summary>
/// Imports from a request in JSON format.
/// Creates a toast request from a JSON string.
/// </summary>
/// <param name="requestJson">Request in JSON format</param>
internal void Import(string requestJson)
/// <returns>Toast request</returns>
public static ToastRequest FromJsonString(string requestJson)
{
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(requestJson)))
{
var serializer = new DataContractJsonSerializer(typeof(ToastRequest));
var buff = (ToastRequest)serializer.ReadObject(stream);

typeof(ToastRequest).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(x => x.CanWrite)
.ToList()
.ForEach(x => x.SetValue(this, x.GetValue(buff)));
return (ToastRequest)serializer.ReadObject(stream);
}
}

/// <summary>
/// Exports a request in JSON format.
/// Converts the request to a JSON string.
/// </summary>
/// <returns>Request in JSON format</returns>
internal string Export()
public string ToJsonString()
{
using (var stream = new MemoryStream())
{
Expand Down