【问题标题】:Deserialization with base class model使用基类模型反序列化
【发布时间】:2021-07-27 11:06:40
【问题描述】:

我需要从mongodb中提取数据,但是这个数据坐标不同,我可以创建一个基子类结构并POST到mongodb,但是GET操作中没有坐标。

    public  class Geometry
    {
        public string type { get; set; }
        
    }
    public class GeoPoly:Geometry
    {

        public  double[][][] coordinates { get; set; }
    }

    public class GeoMultipoly : Geometry
    {
        public  double[][][][] coordinates { get; set; }
    }

我该怎么做

  • 序列化约定是否应该改变以及如何改变
  • 基子类结构适合这个问题吗?

数据库条目:

    {
            "type": "Polygon",
            "coordinates": [
                [
                    [
                        [
                            51.263632,
                            60.962938
                        ],
                        [
                            30.884274,
                            20.065517
                        ],
                        [
                            68.832044,
                            14.362602
                        ],
                        [
                            51.263632,
                            60.962938
                        ]
                    ]
                ]
            ]
    },
{
    "type": "MultiPolygon",
    "coordinates": [
        [
            [
                [
                    43.182162,
                    64.042209
                ],
                [
                    14.721334,
                    22.358269
                ],
                [
                    51.263632,
                    17.738761
                ],
                [
                    43.182162,
                    64.042209
                ]
            ]
        ],
        [
            [
                [
                    55.831419,
                    51.446822
                ],
                [
                    65.66973,
                    20.065517
                ],
                [
                    97.64424,
                    37.509124
                ],
                [
                    55.831419,
                    51.446822
                ]
            ]
        ]
    ]
}

【问题讨论】:

    标签: c# .net .net-core geojson system.text.json


    【解决方案1】:

    我不确定是不是最好的主意, 但是newtonsoftjson.net支持使用$type进行序列化和反序列化。

    项目将以其完整的类标识符保存在数据库中。

    您可以查看here

    一个例子:

    // {
    //   "$type": "Newtonsoft.Json.Samples.Stockholder, Newtonsoft.Json.Tests",
    //   "FullName": "Steve Stockholder",
    //   "Businesses": {
    //     "$type": "System.Collections.Generic.List`1[[Newtonsoft.Json.Samples.Business, Newtonsoft.Json.Tests]], mscorlib",
    //     "$values": [
    //       {
    //         "$type": "Newtonsoft.Json.Samples.Hotel, Newtonsoft.Json.Tests",
    //         "Stars": 4,
    //         "Name": "Hudson Hotel"
    //       }
    //     ]
    //   }
    // }
    

    要使用它,请查看 newtonsoft 文档here

    使用示例:

    Stockholder stockholder = new Stockholder
    {
        FullName = "Steve Stockholder",
        Businesses = new List<Business>
        {
            new Hotel
            {
                Name = "Hudson Hotel",
                Stars = 4
            }
        }
    };
    
    string jsonTypeNameAll = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
    {
        TypeNameHandling = TypeNameHandling.All
    });
    
    

    【讨论】:

      【解决方案2】:

      对于这个 JSON 类正确的做法是:

      public class Rootobject
      {
          public string type { get; set; }
          public float[][][][] coordinates { get; set; }
      }
      

      对于 GeoPoly 和 GeoMultipoly 来说实际上是一样的。您可以分两步进行处理:

      1. 获取列表od Rootobject (List)
      
      switch(g.type)
      {
       case "GeoPoly":
        result.Add(new GeoPoly 
        { 
          type = g.type;
          coordinates  = g.coordinates 
        
        });
        break;
       case "GeoMultipoly":
        result.Add(new GeoMultipoly
        { 
          type = g.type;
          coordinates  = g.coordinates 
        
        });
        break;
        default:
        continue;
      }
      
      

      最后,结果列表将有一个正确类型的几何类型列表。

      【讨论】:

      • 不,如果你想使用地理空间查询,你必须遵守标准。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 2012-10-18
      • 2013-08-26
      相关资源
      最近更新 更多