我应该如何编写这部分代码来序列化所有后代自定义类型?NETJson,如何序列化自定义类型集合中的自定义类型
NETJson问题(https://github.com/rpgmaker/NetJSON)。
下面的代码用于测试它。
NETJson串行执行:
class NETJsonFormatter
{
static bool Initialize()
{
NetJSON.NetJSON.IncludeFields = true;
NetJSON.NetJSON.IncludeTypeInformation = true;
return true;
}
static bool Initialized = Initialize();
/// <summary>
/// Serializza un oggetto in un array di byte.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
[System.Diagnostics.DebuggerStepThrough]
static public byte[] Serialize(object obj)
{
return Encoding.UTF8.GetBytes(NetJSON.NetJSON.Serialize(obj));
}
/// <summary>
/// Trasforma un array di byte nell'oggetto originario.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
[System.Diagnostics.DebuggerStepThrough]
static public object Deserialize(byte[] obj)
{
return NetJSON.NetJSON.Deserialize<object>(Encoding.UTF8.GetString(obj));
}
/// <summary>
/// Deserializza un array di byte nel Type desiderato.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
static public T Deserialize<T>(byte[] obj)
{
return NetJSON.NetJSON.Deserialize<T>(Encoding.UTF8.GetString(obj));
}
}
第一对象序列化:
[Serializable]
public class ComplexType
{
public ComplexType()
{
this.Numero = 100;
this.Stringa = "Contenuto";
}
public int Numero { get; set; }
public string Stringa { get; set; }
}
的第二个目的是序列:
[Serializable]
public class Message_v2 : IMessage
{
public Message_v2()
{
this.Options = new List<string>();
this.Arguments = new Dictionary<string, object>();
}
public bool IsEmpty { get; set; }
public MessageCommand Command { get; set; }
public List<string> Options { get; set; }
/// <summary>
/// Gli Arguments del parser sono sempre KeyValue. Qual'ora mancasse il Value viene inserito null.
/// </summary>
public Dictionary<string, object> Arguments { get; set; }
/*
* Public methods
*/
public void AddOptions(params string[] options)
{
foreach (string option in options)
this.Options.Add(option);
}
public void AddArgument(string key, object value)
{
this.Arguments.Add(key, value);
}
public byte[] ToArray()
{
return NETJsonFormatter.Serialize(this);
}
public string ToXML()
{
throw new NotImplementedException();
}
/// <summary>
/// For debugging purposes.
/// </summary>
/// <returns></returns>
public string ToJSON()
{
return Encoding.UTF8.GetString(NETJsonFormatter.Serialize(this));
}
/*
* Conversions
*/
public static explicit operator Message_v2(byte[] source)
{
try
{
return NETJsonFormatter.Deserialize<Message_v2>(source);
}
catch
{
return null;
}
}
}
和失败的单元测试。 第一个测试,ComplexObject上的测试通过。为了确保数据的一致性,我使用了DeepEqual(https://github.com/jamesfoster/DeepEqual - NUGet上的'DeepEqual'),它提供了用于对象比较的.ShouldDeepEqual方法。
[TestMethod]
public void CreateAndRetrieveMessage()
{
ComplexType complexArgument = new ComplexType();
byte[] serializedComplexArgument = NETJsonFormatter.Serialize(complexArgument);
ComplexType deserializedComplexArgument = NETJsonFormatter.Deserialize<ComplexType>(serializedComplexArgument);
deserializedComplexArgument.ShouldDeepEqual(complexArgument);
/* ------------------------ */
IMessage message = ProtocolHelper.CreateMessage();
message.Command = MessageCommand.Set;
message.AddOptions("keys");
message.AddArgument("Key1", "Contenuto");
message.AddArgument("Key2", 100);
message.AddArgument("Key3", complexArgument);
// Send over the wire.
byte[] serialized = message.ToArray();
// Get the Message sent.
var deserialized = ProtocolHelper.CreateMessage(serialized);
deserialized.ShouldDeepEqual(message);
}
这听起来不错。但我的问题是我的Message_v2.Arguments是动态的,我不知道或控制哪些类型的用户选择输入它们。 所以我无法将Message_v2.Arguments的声明从Dictionary更改为Dictionary 。 事实上,在我的单元测试中,我明确地写了三个值,一个int,一个字符串,一个ComplexType。 –
Lorenzo
也许使用其他序列化程序,newtonsoft可以很好地用于这个用例,我编辑了我的文章 – Mathieu
性能的改进是使用NETJson的关键。 其实我使用BinaryFormatter来进行序列化并通过线路发送并运行良好。我已经用NewtonSoft“玩”了Json序列化,没有任何问题。 所以,你是对的,如果我想在Json上序列化,我会使用更强大的json序列化器,但是我使用标记“NETJson”打开了问题,并且stackoverflow.com确实响应了“你不能使用开始问题标记NETJson如果你没有1500声望点“。 Eh。 :( – Lorenzo