gpt4 book ai didi

c# - DataContractJsonSerializer 生成哈希列表而不是哈希

转载 作者:行者123 更新时间:2023-11-30 12:53:22 24 4
gpt4 key购买 nike

我希望有一个 Dictionary 对象的形式:

var dict = new Dictionary<string,string>()
{
{"blah", "bob"},
{"blahagain", "bob"}
};

以以下形式序列化成JSON:

{ "blah": "bob", "blahagain": "bob" }

[ { "key": "blah", "value": "bob" }, { "key": "blahagain", "value": "bob"}]

为什么看起来是一种怪异的序列化集合的通用尝试?

DataContractJsonSerializer使用ISerializable接口(interface)来产生这个东西。在我看来,好像有人从 ISerializable 中获取了 XML 输出并从中删除了这个东西。

这里有办法覆盖.Net 使用的默认序列化吗?我可以只从 Dictionary 派生并覆盖序列化方法吗?

张贴以听取人们可能有的任何警告或建议。

最佳答案

好吧,我决定回避 Dictionary 对象的 DataContract 序列化。

我在我的根对象中创建了两个虚拟方法。

public virtual void prepareForSerialization();
public virtual void postDeserialize();

然后为我的类的每个字典属性指定字符串 DataMember 类属性。字符串被序列化,字典不再直接序列化。

[DataMember]
public string dictionaryString;
public Dictionary<int, string> dict;

然后,当我的代码调用序列化时,它还会首先调用 prepareForSerialization。

public override void prepareForSerialization() { 
base.prepareForSerialization();
}

public override void postDeSerialize() {
base.postDeSerialize();
}

具有 Dictionary 成员的派生类将调用我自己的 Dictionary 序列化器。

注意:这是一个适合我目的的简单序列化。 YMMV。Javascript 将功劳归功于另一篇 stackoverfow 帖子。忘记是哪一个了。

/// <summary>
/// Extension methods needed to implement Javascript dates for C#
/// </summary>
public static class MyExtensions{
public static double JavascriptTicks(this DateTime dt) {
DateTime d1 = new DateTime(1970, 1, 1);
DateTime d2 = dt.ToUniversalTime();
TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
return ts.TotalMilliseconds;
}
}
/// <summary>
/// Serialize a single value
/// </summary>
/// <param name="o">An object to serialize</param>
/// <returns>A JSON string of the value</returns>
if (o is string) {
return string.Format("\"{0}\"", o);
} else if (o is DateTime) {
return string.Format("new Date({0})", ((DateTime)o).JavascriptTicks()); ;
} else if(o.GetType().IsValueType) {
return o.ToString();
} else {
//Here you want a check of the form if is IMySerializer, then call your prepare before
//using the .Net one.
DataContractJsonSerializer json = new DataContractJsonSerializer(o.GetType());
using(MemoryStream ms = new MemoryStream())
using (StreamReader sr = new StreamReader(ms)) {
json.WriteObject(ms, o);
ms.Position = 0;
return sr.ReadToEnd();
}
}

/// <summary>
/// Serializes a List object into JSON
/// </summary>
/// <param name="list">The IList interface into the List</param>
/// <returns>A JSON string of the list</returns>
public string SerializeList(IList list) {
string result = null;
if (list != null) {
IEnumerator it = list.GetEnumerator();
StringBuilder sb = new StringBuilder();
long len = list.Count;
sb.Append("[");
while (it.MoveNext()) {
if (it.Current is IList) {
sb.Append(SerializeList((IList)it.Current));
} else if (it.Current is IDictionary) {
sb.Append(SerializeDictionary((IDictionary)it.Current));
} else {
sb.Append(SerializeValue(it.Current));
}
--len;
if (len > 0) sb.Append(",");
}
sb.Append("]");
result = sb.ToString();
}
return result;
}
/// <summary>
/// Returns a stringified key of the object
/// </summary>
/// <param name="o">The key value</param>
/// <returns></returns>
public string SerializeKey(object o) {
return string.Format("\"{0}\"", o);
}
/// <summary>
/// Serializes a dictionary into JSON
/// </summary>
/// <param name="dict">The IDictionary interface into the Dictionary</param>
/// <returns>A JSON string of the Dictionary</returns>
public string SerializeDictionary(IDictionary dict) {
string result = null;
if (dict != null) {
IDictionaryEnumerator it = dict.GetEnumerator();
StringBuilder sb = new StringBuilder();
long len = dict.Count;
sb.Append("{");
while (it.MoveNext()) {
sb.Append(SerializeKey(it.Key));
sb.Append(":");
if (it.Value is IList) {
sb.Append(SerializeList((IList)it.Value));
} else if (it.Value is IDictionary) {
sb.Append(SerializeDictionary((IDictionary)it.Value));
} else {
sb.Append(SerializeValue(it.Value));
}
--len;
if (len > 0) sb.Append(",");
}
sb.Append("}");
result = sb.ToString();
}
return result;
}

关于c# - DataContractJsonSerializer 生成哈希列表而不是哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2526335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com