首页 > 其他分享 >Json.Net Deserialize a Collection from BSON

Json.Net Deserialize a Collection from BSON

时间:2023-12-21 15:44:20浏览次数:36  
标签:Deserialize Collection Json reader new BSON public

Deserialize a Collection from BSON (newtonsoft.com)

This sample sets ReadRootValueAsArray to true so the root BSON value is correctly read as an array instead of an object and deserializes BSON to a collection.

Sample Types Copy
public class Event
{
    public string Name { get; set; }
    public DateTime StartDate { get; set; }
}
Usage Copy
string s = "MQAAAAMwACkAAAACTmFtZQAHAAAARWFzdGVyAAlTdGFydERhdGUAgDf0uj0BAAAAAA==";
byte[] data = Convert.FromBase64String(s);

MemoryStream ms = new MemoryStream(data);
using (BsonReader reader = new BsonReader(ms))
{
    reader.ReadRootValueAsArray = true;

    JsonSerializer serializer = new JsonSerializer();

    IList<Event> events = serializer.Deserialize<IList<Event>>(reader);

    Console.WriteLine(events.Count);
    // 1

    Console.WriteLine(events[0].Name);
    // Easter
}
 

标签:Deserialize,Collection,Json,reader,new,BSON,public
From: https://www.cnblogs.com/webenh/p/17919230.html

相关文章

  • cjson 用法
     1、修改字典的值cJSON_SetValuestring(objectItem,value)//先获取objectTempPtr=cJSON_GetObjectItem(TempPtr,"nm");//修改该object的值cJSON_SetValuestring(TempPtr,"guxiangdehai");2、删除数组里面的值cJSON_DeleteItemFromArray(Array,index);//Array要......
  • fastjson2
    什么是JSONJSON是一种轻量级的数据交换格式。它基于ECMAScript的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构是的JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。JSON语法使用大括号......
  • Fastjson2基础使用以及底层序列化/反序列化实现探究
    来自于:https://www.cnblogs.com/6b7b5fc3/p/17134421.html1Fastjson2简介Fastjson2是Fastjson的升级版,特征:协议支持:支持JSON/JSONB两种协议部分解析:可以使用JSONPath进行部分解析获取需要的值语言支持:Java/Kotlin场景支持:Android8+/服务端其他特性支持:GraalNative-Image......
  • json.NewDecode用法
    Go使用RFC3339进行编码,如果控制生成的json,只需将2022-04-03T00:00:00.000更改为2022-04-03T00:00:00.000Z。例如,这是有效的。typeContractstruct{ContractId*int`json:"contract_id"`CompanyId*int`json:"company_id"`Dat......
  • OS模块和JSON模块
    OS模块和JSON模块【一】OS模块【二】JSON模块【1】序列化与反序列化序列化和反序列化是计算机科学中数据处理中的两个过程,用于将数据从一种格式转换为另一种格式。这两个过程通常称为编码和解码。序列化是将数据结构或对象转换为字节序列的过程,使得数据可以以一种易于传输......
  • Collections
    JDK8.0对集合进行有优化Collection表示一组对象,这些对象也称为collection的元素。|List:元素可以重复;保证插入顺序和取出顺序一致|ArrayList:数组结构查询快,增删效率略低线程不安全的容量不够时,按自身的50%进行扩容10--->15--->22JDK8.0与7.0中......
  • c# - 如何在自定义 System.Text.Json JsonConverter 中使用默认序列化?
    我正在写一个 custom System.Text.Json.JsonConverter 将旧数据模型升级到新版本。我已覆盖 Read()并实现了必要的后处理。但是,我根本不需要在 Write() 中做任何自定义操作。方法。如果我根本没有转换器,如何自动生成默认序列化?显然我可以使用不同的 JsonSerializerOption......
  • 从Newtonsoft.Json迁移到 System.Text.Json不简单
    一.写在前面#System.Text.Json是.NETCore3及以上版本内置的Json序列化组件,刚推出的时候经常看到踩各种坑的吐槽,现在经过几个版本的迭代优化,提升了易用性,修复了各种问题,是时候考虑使用System.Text.Json了。本文将从使用层面来进行对比。System.Text.Json在默认情况下十......
  • .net C# System.Text.Json 如何将 string类型的“true”转换为布尔值 解决方案
    直接上解决方法的代码先定义一个转换顺,代码如下:publicsealedclassAnhBoolConverter:JsonConverter<bool?>{publicoverridebool?Read(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){varval......
  • Collections工具类的使用
    packagebackend02;//Collections工具类的使用importjava.util.ArrayList;importjava.util.Collections;publicclassPractice07{publicstaticvoidmain(String[]args){ArrayList<String>list=newArrayList<>();//语法格式:......