Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
json: fix option sumtype handling (#20186)
- Loading branch information
Showing
2 changed files
with
83 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import json | ||
|
||
type Prices = Price | []Price | ||
|
||
pub struct ShopResponseData { | ||
attributes Attributes | ||
} | ||
|
||
struct Attributes { | ||
price ?Prices | ||
} | ||
|
||
struct Price { | ||
net f64 | ||
} | ||
|
||
fn test_main() { | ||
data := '{"attributes": {"price": [{"net": 1, "_type": "Price"}, {"net": 2, "_type": "Price"}]}}' | ||
entity := json.decode(ShopResponseData, data) or { panic(err) } | ||
assert entity == ShopResponseData{ | ||
attributes: Attributes{ | ||
price: Prices([Price{ | ||
net: 1 | ||
}, Price{ | ||
net: 2 | ||
}]) | ||
} | ||
} | ||
|
||
data2 := '{"attributes": {"price": {"net": 1, "_type": "Price"}}}' | ||
entity2 := json.decode(ShopResponseData, data2) or { panic(err) } | ||
assert entity2 == ShopResponseData{ | ||
attributes: Attributes{ | ||
price: Prices(Price{ | ||
net: 1 | ||
}) | ||
} | ||
} | ||
|
||
data3 := json.encode(ShopResponseData{ | ||
attributes: Attributes{ | ||
price: Prices([Price{ | ||
net: 1.2 | ||
}]) | ||
} | ||
}) | ||
assert data3 == '{"attributes":{"price":[{"net":1.2,"_type":"Price"}]}}' | ||
|
||
entity3 := json.decode(ShopResponseData, data3) or { panic(err) } | ||
assert entity3 == ShopResponseData{ | ||
attributes: Attributes{ | ||
price: Prices([Price{ | ||
net: 1.2 | ||
}]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters