Json.net how to serialize object as value(Json.net 如何将对象序列化为值)
问题描述
我仔细阅读了文档、StackOverflow 等,似乎找不到这个...
I've pored through the docs, StackOverflow, etc., can't seem to find this...
我想做的是将一个简单的值类型的对象序列化/反序列化为一个值,而不是一个对象,如下所示:
What I want to do is serialize/deserialize a simple value-type of object as a value, not an object, as so:
public class IPAddress
{
byte[] bytes;
public override string ToString() {... etc.
}
public class SomeOuterObject
{
string stringValue;
IPAddress ipValue;
}
IPAddress ip = new IPAddress("192.168.1.2");
var obj = new SomeOuterObject() {stringValue = "Some String", ipValue = ip};
string json = JsonConverter.SerializeObject(obj);
我想要的是让 json 像这样序列化:
What I want is for the json to serialize like this:
// json = {"someString":"Some String","ipValue":"192.168.1.2"} <- value serialized as value, not subobject
不是 ip 成为嵌套对象的地方,例如:
Not where the ip becomes a nested object, ex:
// json = {"someString":"Some String","ipValue":{"value":"192.168.1.2"}}
有人知道怎么做吗?谢谢!(P.S. 我在一个庞大的遗留 .NET 代码库上使用 Json 序列化,所以我无法真正更改任何现有类型,但我可以扩充/分解/装饰它们以促进 Json 序列化.)
Does anyone know how to do this? Thanks! (P.S. I am bolting Json serialization on a large hairy legacy .NET codebase, so I can't really change any existing types, but I can augment/factor/decorate them to facilitate Json serialization.)
推荐答案
您可以使用 IPAddress
类的自定义 JsonConverter
来处理这个问题.这是您需要的代码:
You can handle this using a custom JsonConverter
for the IPAddress
class. Here is the code you would need:
public class IPAddressConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IPAddress));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return new IPAddress(JToken.Load(reader).ToString());
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
JToken.FromObject(value.ToString()).WriteTo(writer);
}
}
然后,将 [JsonConverter]
属性添加到您的 IPAddress
类中,您就可以开始了:
Then, add a [JsonConverter]
attribute to your IPAddress
class and you're ready to go:
[JsonConverter(typeof(IPAddressConverter))]
public class IPAddress
{
byte[] bytes;
public IPAddress(string address)
{
bytes = address.Split('.').Select(s => byte.Parse(s)).ToArray();
}
public override string ToString()
{
return string.Join(".", bytes.Select(b => b.ToString()).ToArray());
}
}
这是一个工作演示:
class Program
{
static void Main(string[] args)
{
IPAddress ip = new IPAddress("192.168.1.2");
var obj = new SomeOuterObject() { stringValue = "Some String", ipValue = ip };
string json = JsonConvert.SerializeObject(obj);
Console.WriteLine(json);
}
}
public class SomeOuterObject
{
public string stringValue { get; set; }
public IPAddress ipValue { get; set; }
}
输出:
{"stringValue":"Some String","ipValue":"192.168.1.2"}
这篇关于Json.net 如何将对象序列化为值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Json.net 如何将对象序列化为值
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 使用 rss + c# 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01