解析具有相同属性中多个表示形式的JSON [英] Parsing JSON with multiple representation in the same attribute

查看:148
本文介绍了解析具有相同属性中多个表示形式的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Rust相对较新,对Serde则相对较新,因此我很难确定这是否可行.我有一个JSON文件,该文件具有相同密钥的两种不同表示形式:

I'm relatively new to Rust, and even more so to Serde, so I'm having trouble finding out if this is even doable. I have a JSON file which has two different representations for the same key:

"coordinates": [
  [
    [
      121.423364,
      24.9913596
    ],
    [
      121.4233327,
      24.9912977
    ],
  ]
]

和这个:

"coordinates": [
  [
    121.4472492,
    25.0052053
  ],
  [
    121.4466457,
    25.0028547
  ]
]

在同一属性中有一个二维数组和一个三维数组表示方式.这使文件难以序列化.

There is a two dimensional array and a three dimensional array representing ways in the same attribute. This makes the file hard to serialize.

这是我实现的代码:

#[derive(Serialize, Deserialize, Debug)]
struct Geometry {
    #[serde(deserialize_with = "string_or_number", rename = "type")]
    geometry_type: Value,
    #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "coordinates")]
    geometry_coor: Vec<Coordinates>,
    #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "coordinates")]
    geometry_coor2: Vec<Vec<Coordinates>>,
}

#[derive(Serialize, Deserialize, Debug)]
struct Coordinates {
    #[serde(deserialize_with = "string_or_number")]
    longitude: Value,
    #[serde(deserialize_with = "string_or_number")]
    latitude: Value,
}

fn string_or_number<'de, D>(de: D) -> Result<Value, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let helper: Value = Deserialize::deserialize(de)?;

    match helper {
        Value::Number(n) => {
            println!("{:#?}", n.as_f64().unwrap().to_string());
            Ok(Value::Number(n))
        }
        Value::String(s) => Ok(json!(s)),
        _ => Ok(json!(null)),
    }
}

我在struct Geometry序列化坐标文件时遇到麻烦.

I have trouble in struct Geometry which serializes the file of coordinates.

我有什么方法可以处理这种形式?

Are there any ways that I can deal with this kind of form?

推荐答案

我得到了帮助来自serde-rs开发人员:

我建议使用未标记的枚举表示可以是2d或3d. 游乐场链接

I would recommend using an untagged enum to represent a coordinate array that can be 2d or 3d. Playground link

这是修改后的代码:

#[derive(Serialize, Deserialize, Debug)]
struct Geometry {
    #[serde(deserialize_with = "string_or_number", rename = "type")]
    geometry_type: Value,
    #[serde(default, skip_serializing_if = "Vec::is_empty", rename = "coordinates")]
    geometry_coor: Vec<Coordinates_form>,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
enum Coordinates_form {
    #[serde(skip_serializing)]
    OneD(Coordinates),
    #[serde(skip_serializing)]
    TwoD(Vec<Coordinates>),
    #[serde(skip_serializing)]
    ThreeD(Vec<Vec<Coordinates>>),
}

#[derive(Deserialize, Debug)]
struct Coordinates {
    #[serde(deserialize_with = "string_or_number")]
    longitude: Value, 
    #[serde(deserialize_with = "string_or_number")]
    latitude: Value,
}

fn string_or_number<'de, D>(de: D) -> Result<Value, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let helper: Value = Deserialize::deserialize(de)?;

    match helper {
        Value::Number(n) => {
            println!("{:#?}", n.as_f64().unwrap().to_string());
            Ok(Value::Number(n))
        }
        Value::String(s) => Ok(json!(s)),
        _ => Ok(json!(null)),
    }
}

这篇关于解析具有相同属性中多个表示形式的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆