前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JToken和BsonValue对象的相互转换

JToken和BsonValue对象的相互转换

作者头像
莫问今朝
发布2019-03-01 15:38:53
1.2K0
发布2019-03-01 15:38:53
举报
文章被收录于专栏:博客园
代码语言:javascript
复制
 /// <summary>
    ///         JObject和BsonValue之间的
    /// </summary>
    public static class AdapterExtensions
    {

       /// <summary>
       ///      
       /// </summary>
       /// <param name="object"></param>
       /// <returns></returns>
        public static BsonDocument ToBsonDocument(this JObject @object)
        {
            IDictionary<String, Object> elems = new Dictionary<String, Object>(); ;
            foreach (var item in @object)
                elems[item.Key] = ToBsonValue(item.Value);

            return new BsonDocument(elems);
        }
        public static BsonArray ToBsonArray(this JArray array)
        {
            IList<BsonValue> bvs = new List<BsonValue>();
            foreach (JToken item in array)
                bvs.Add(ToBsonValue(item));
            return new BsonArray(bvs);
        }
        private static BsonValue ToBsonValue(this JToken val)
        {
            if (val is JArray)
                return ToBsonArray(val as JArray);
            if (val is JObject)
                return ToBsonDocument(val as JObject);
            else
                return ToBasicValue(val);
        }
       
        private static BsonValue ToBasicValue(JToken @object)
        {
            switch (@object.Type)
            {
                case JTokenType.Integer:
                    return BsonValue.Create(@object.ToObject<Int32>());
                case JTokenType.Float:
                    return BsonValue.Create(@object.ToObject<float>());
                case JTokenType.String:
                    return BsonValue.Create(@object.ToObject<String>());
                case JTokenType.Boolean:
                    return BsonValue.Create(@object.ToObject<Boolean>());
                case JTokenType.Date:
                    return BsonValue.Create(@object.ToObject<DateTime>());
                case JTokenType.Guid:
                    return BsonValue.Create(@object.ToObject<Guid>());
                case JTokenType.Null:
                    return null;
                default:
                    throw new Exception("");
            }
        }


        public static JObject ToJObject(this BsonDocument bsonDocument)
        {
            JObject elems = new JObject();
            foreach (var item in bsonDocument)
            {
                Object data = item.Value;
                elems[item.Name] = ToJsonValue(item.Value);
            }
            return elems;
        }
        public static JArray ToJArray(this BsonArray array)
        {
            IList<JToken> bvs = new List<JToken>();
            foreach (var item in array)
                bvs.Add(ToJsonValue(item));
            return new JArray(bvs);
        }
        private static JToken ToJsonValue(this BsonValue val)
        {
            if (val is BsonArray)
                return ToJArray(val as BsonArray);
            if (val is BsonDocument)
                return ToJObject(val as BsonDocument);
            if (val is BsonNull)
                return null;
            return JToken.FromObject(val);
        }
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-01-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档