简体   繁体   中英

How to serialize dictionary<object,object> to json

I have:

1)

Dictionary<MyClass, double>

2) elasticsearch 7

3) nest 7

I want to write my dictionary in elasticsearch database by:

client.IndexDocument(myDictionary).

I know, I can use

List<KeyValuePair<MyClass, double>>

But in my work I need use Dictionary.

You need to implement a type converter eg. MyClassConverter for MyClass and then add the attribute [TypeConverter(typeof(MyClassConverter))] to the MyClass class declaration. This means that, instead of using the default ToString(), it will use the type convert that you define to serialize as you want.

The link "How to: Implement a Type Converter" shows how to create a type converter.

The SO link showing the answer is: Not ableTo Serialize Dictionary with Complex key using Json.net

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM