Problem: I want to read and serve JSON, but the JSON structure differs by query. I solve this with an interface FleetVehicle, a base Vehicle struct, and additional specialized structs that allow for differetnail parsing. The Fleet struct holds raw json in RawVehicles, and parsed objects/structs in Vehicles. By writing MarsahJSON and UnmarshalJSON for the Fleet struct, and I control how each JSON block is parsed into a different type of struct/vehicle. To run: GOPATH=${PWD} go run helloVariant.go
, and check http://localhost:8080/world
. This loads multiple JSON files into one Fleet object allowing me to serve a concatenation of the JSON files. For now, each JSON block must use the data
field to hold a list of vehicles.
- helloVariant.go is the main file
- src/varparse/varparse.go holds structs for variants in JSON
- testVariant1.json and testVariant2.json are example files used by helloVariant.go
- hello.go is my initial attempt
- fleet.go, fleet_test.go, fleet_test_