Hello everyone,
Please I have a data model (called GeoLocation) with mutually exclusive fields (i.e point and region. Only one field must be present in the struct when it is returned) that I need to convert to JSON using serde. How do I implement this data structure in the most efficient way?
The data model for GeoLocation:
GeoLocation
point:Ellipse
region:Polygon
confidence:int // optional field
Note: point and region are mutually exclusive. Exactly one must be present.
My idea
struct Ellipse {}
struct Polygon {}
struct GeoLocation {
union mutExField{ // How do I make the Union nameless?
point: Ellipse, // Mutually exclusive
region: Polygon, // Mutually exclusive
}
confidence: Option<u32>, // OPTIONAL field
}
impl GeoLocation {
fn to_json() -> String {
let geo = GeoLocation {
point: Ellipse::new(), // I don't want the Union name to appear in the JSON string
confidence: None
};
let s = serde_json::to_string_pretty(&geo).unwrap();
return s;
}
}