将BsonArray转换为List <t>

时间:2016-06-20 21:59:24

标签: c# mongodb

我一直在努力,并设法将json解析为C#对象。现在,下一步是将json数组解析为List。我已经设法完成工作,但我非常确定有更好的方法将BsonArray转换为List

using (StreamReader file = File.OpenText(filename))
{
   var jsonSampleData = file.ReadToEnd();                    
   _thePlan = BsonSerializer.Deserialize<List<ThePlan>>(jsonSampleData);
}

编辑 - 附加信息

要清楚我需要完成的是获取给定的json文档并将其重新水合为List。由于我是mongo的新手并且T是一个mongo实体代表,这使事情变得更加复杂。

正如安德烈指出它运作良好:

{
   "_id": "57509afbc6b48d3f33b2dfcd",
   ...
}

考虑到我昨天的挣扎,我认为这实际上与我的json有关,我的早期尝试看起来像这样:

{
   "_id": { "$oid": "57509afbc6b48d3f33b2dfcd" },
   .....
}

在弄清楚这一切的过程中,我的json成熟了:

$date = '2016-06-20';
$time = '17:30:00';
$combinedDateTime = $date . ' ' . $time;
$time = date("m/d/Y h:i:s A T", strtotime($combinedDateTime));
$time =  new \DateTime($time);

我和BsonSerializer一起遇到的麻烦很可能是我的坏json,一旦解决了这个问题,我就不会精力回到BsonSerielizer再试一次。

1 个答案:

答案 0 :(得分:1)

要么一直强力输入,要么根本不打字。

强类型

假设这些是你的类型:

public class BaseObject {
  [BsonId] public ObjectId id { get; set; }
  [BsonElement("plans")] public List<ThePlan> Plans { get; set; }
}
public class ThePlan {
  [BsonElement("i")] public int Integer { get; set; }
  [BsonElement("s")] public string String { get; set; }
}

和这些测试工具:

void ToJsonTyped(BaseObject bo)
{
  var sb = new StringBuilder();
  using (TextWriter tw = new StringWriter(sb))
  using (BsonWriter bw = new JsonWriter(tw)) 
  {
    BsonSerializer.Serialize<BaseObject>(bw, bo);
  }
  string jsonObject = sb.ToString();
  BaseObject bo2 = BsonSerializer.Deserialize<BaseObject>(jsonObject);
  Assert.AreEqual(bo, bo2);
}

void ToBsonTyped(BaseObject bo)
{
  byte[] bsonObject = null;
  using (var ms = new MemoryStream())
  using (BsonWriter bw = new BsonBinaryWriter(ms))
  {
    BsonSerializer.Serialize<BaseObject>(bw, bo);
    bsonObject = ms.ToArray();
  }
  BaseObject bo1 = BsonSerializer.Deserialize<BaseObject>(bsonObject);
  Assert.AreEqual (bo, bo1);
}

你可以测试:

  BaseObject bo = new BaseObject() {
    Plans = new List<ThePlan>() {
    new ThePlan() {Integer=1, String="one" },
    new ThePlan() {Integer=2, String="two" },
    new ThePlan() {Integer=3, String="three" } } };
  ToBsonTyped(bo);
  ToJsonTyped(bo);

根本没有输入BsonDocumentBsonArray

的组合

测试:

  BsonDocument doc = new BsonDocument();
  var bsonArray = new BsonArray();
  bsonArray.Add(new BsonDocument("one", 1));
  bsonArray.Add(new BsonDocument("two", 2));
  bsonArray.Add(new BsonDocument("three", 3));
  doc.Add( new BsonElement("plans", bsonArray));
  ToBsonUnTyped(doc);
  ToJsonUnTyped(doc);

test utils:

void ToBsonUnTyped(BsonDocument doc) {
  byte[] bsonObject = null;
  using (var ms = new MemoryStream())
  using (BsonWriter bw = new BsonBinaryWriter(ms))
  {
    BsonSerializer.Serialize<BsonDocument>(bw, doc);
    bsonObject = ms.ToArray();
  }
  BsonDocument docActual = BsonSerializer.Deserialize<BsonDocument>(bsonObject);
  Assert.AreEqual (doc, docActual);
}

void ToJsonUnTyped(BsonDocument doc)
{
  var sb = new StringBuilder();
  using (TextWriter tw = new StringWriter(sb))
  using (BsonWriter bw = new JsonWriter(tw))
  {
    BsonSerializer.Serialize<BsonDocument>(bw, doc);        
  }
  string jsonObject = sb.ToString();
  BsonDocument doc2 = BsonSerializer.Deserialize<BsonDocument>(jsonObject);
  Assert.AreEqual(doc, doc2);
}