Skip to content

Commit 149339c

Browse files
author
Stepan Kuzmin
authored
fix: skip serializing if Option is None (#6)
1 parent 20e9e7c commit 149339c

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/tilejson.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ pub struct TileJSON {
1111
pub tilejson: &'static str,
1212

1313
/// The tileset id.
14+
#[serde(skip_serializing_if = "Option::is_none")]
1415
pub id: Option<String>,
1516

1617
/// A name describing the tileset. The name can
1718
/// contain any legal character. Implementations SHOULD NOT interpret the
1819
/// name as HTML.
20+
#[serde(skip_serializing_if = "Option::is_none")]
1921
pub name: Option<String>,
2022

2123
/// A text description of the tileset. The
2224
/// description can contain any legal character. Implementations SHOULD NOT
2325
/// interpret the description as HTML.
26+
#[serde(skip_serializing_if = "Option::is_none")]
2427
pub description: Option<String>,
2528

2629
/// A semver.org style version number. When
@@ -30,29 +33,34 @@ pub struct TileJSON {
3033
/// level MUST only have changes to tiles that are contained within one tile.
3134
/// When tiles change significantly, the major version MUST be increased.
3235
/// Implementations MUST NOT use tiles with different major versions.
36+
#[serde(skip_serializing_if = "Option::is_none")]
3337
pub version: Option<String>,
3438

3539
/// Contains an attribution to be displayed
3640
/// when the map is shown to a user. Implementations MAY decide to treat this
3741
/// as HTML or literal text. For security reasons, make absolutely sure that
3842
/// this field can't be abused as a vector for XSS or beacon tracking.
43+
#[serde(skip_serializing_if = "Option::is_none")]
3944
pub attribution: Option<String>,
4045

4146
/// Contains a mustache template to be used to
4247
/// format data from grids for interaction.
4348
/// See https://github.com/mapbox/utfgrid-spec/tree/master/1.2
4449
/// for the interactivity specification.
50+
#[serde(skip_serializing_if = "Option::is_none")]
4551
pub template: Option<String>,
4652

4753
/// Contains a legend to be displayed with the map.
4854
/// Implementations MAY decide to treat this as HTML or literal text.
4955
/// For security reasons, make absolutely sure that this field can't be
5056
/// abused as a vector for XSS or beacon tracking.
57+
#[serde(skip_serializing_if = "Option::is_none")]
5158
pub legend: Option<String>,
5259

5360
/// Either "xyz" or "tms". Influences the y
5461
/// direction of the tile coordinates.
5562
/// The global-mercator (aka Spherical Mercator) profile is assumed.
63+
#[serde(skip_serializing_if = "Option::is_none")]
5664
pub scheme: Option<String>,
5765

5866
/// An array of tile endpoints. {z}, {x} and {y}, if present,
@@ -69,6 +77,7 @@ pub struct TileJSON {
6977
/// for this tileset.
7078
/// See https://github.com/mapbox/utfgrid-spec/tree/master/1.2
7179
/// for the interactivity specification.
80+
#[serde(skip_serializing_if = "Option::is_none")]
7281
pub grids: Option<Vec<String>>,
7382

7483
/// An array of data files in GeoJSON format.
@@ -78,18 +87,22 @@ pub struct TileJSON {
7887
/// All endpoints MUST return the same content for the same URL.
7988
/// If the array doesn't contain any entries, then no data is present in
8089
/// the map.
90+
#[serde(skip_serializing_if = "Option::is_none")]
8191
pub data: Option<Vec<String>>,
8292

8393
/// An integer specifying the minimum zoom level.
94+
#[serde(skip_serializing_if = "Option::is_none")]
8495
pub minzoom: Option<u8>,
8596

8697
/// An integer specifying the maximum zoom level. MUST be >= minzoom.
98+
#[serde(skip_serializing_if = "Option::is_none")]
8799
pub maxzoom: Option<u8>,
88100

89101
/// The maximum extent of available map tiles. Bounds MUST define an area
90102
/// covered by all zoom levels. The bounds are represented in WGS:84
91103
/// latitude and longitude values, in the order left, bottom, right, top.
92104
/// Values may be integers or floating point numbers.
105+
#[serde(skip_serializing_if = "Option::is_none")]
93106
pub bounds: Option<Vec<f32>>,
94107

95108
/// The first value is the longitude, the second is latitude (both in
@@ -99,6 +112,7 @@ pub struct TileJSON {
99112
/// Implementations can use this value to set the default location. If the
100113
/// value is null, implementations may use their own algorithm for
101114
/// determining a default location.
115+
#[serde(skip_serializing_if = "Option::is_none")]
102116
pub center: Option<Vec<i32>>,
103117
}
104118

@@ -130,7 +144,7 @@ impl TileJSONBuilder {
130144
name: None,
131145
description: None,
132146
version: Some("1.0.0".to_owned()),
133-
attribution: Some("".to_string()),
147+
attribution: None,
134148
template: None,
135149
legend: None,
136150
scheme: Some("xyz".to_owned()),
@@ -299,7 +313,7 @@ mod tests {
299313

300314
assert_eq!(
301315
serialized_tilejson,
302-
r#"{"tilejson":"2.2.0","id":null,"name":"compositing","description":null,"version":"1.0.0","attribution":"","template":null,"legend":null,"scheme":"tms","tiles":["http://localhost:8888/admin/1.0.0/world-light,broadband/{z}/{x}/{y}.png"],"grids":null,"data":null,"minzoom":0,"maxzoom":30,"bounds":[-180.0,-90.0,180.0,90.0],"center":null}"#
316+
r#"{"tilejson":"2.2.0","name":"compositing","version":"1.0.0","scheme":"tms","tiles":["http://localhost:8888/admin/1.0.0/world-light,broadband/{z}/{x}/{y}.png"],"minzoom":0,"maxzoom":30,"bounds":[-180.0,-90.0,180.0,90.0]}"#
303317
)
304318
}
305319
}

0 commit comments

Comments
 (0)