BIM World
A Professional BIM Learning Platform


Custom Serialization Fields in Newtonsoft.Json by Johnson

Revit Secondary Development: Newtonsoft.Json Custom Serialization Fields

Below is an example demonstrating how to customize JSON serialization using Newtonsoft.Json in a Revit secondary development context.

class Program

{

static void Main(string[] args)

{

var model = new JsonModel()

{

Id = 0,

Name = "Nnnn osen"

};

var json = JsonConvert.SerializeObject(model, new JsonSerializerSettings()

{

ContractResolver = new MyContractResolver(new List<string>() { "Id" })

});

Console.WriteLine(json);

}

}

public class JsonModel

{

[JsonProperty("myId")]

public int Id { get; set; }

public string Name { get; set; }

}

public class MyContractResolver : DefaultContractResolver

{

private List<string> _names = null;

public MyContractResolver(List<string> names)

{

_names = names;

}

protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)

{

string name = member.Name;

if (_names.Contains(name))

return base.CreateProperty(member, memberSerialization);

return null;

}

}

xuebim
Follow the latest BIM developments in the architecture industry, explore innovative building technologies, and discover cutting-edge industry insights.
← Scan with WeChat
Like(0) 打赏
BIM WORLD » Custom Serialization Fields in Newtonsoft.Json by Johnson

Comment Get first!

Must log in before commenting!

 

BIM World, A Professional BIM Learning Platform

Stay updated on the latest architecture trends and share new building technologies.

Contact UsAbout Us

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

Account Login

By signing in, you agree toUser Agreement

Sign Up