Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmarshaling of Overpass JSON fails on version field #44

Closed
fawick opened this issue Nov 17, 2022 · 5 comments
Closed

Unmarshaling of Overpass JSON fails on version field #44

fawick opened this issue Nov 17, 2022 · 5 comments

Comments

@fawick
Copy link

fawick commented Nov 17, 2022

The version information is encoded as JSON number instead of a string by Overpass API. This causes unmarshaling it to fail with error json: cannot unmarshal number into Go struct field .version of type string

Take, for example, the minimal JSON output of overpass (http://overpass-api.de/api/interpreter?data=[out:json];out;), e.g.

{
  "version": 0.6,
  "generator": "Overpass API 0.7.59 e21c39fe",
  "osm3s": {
    "timestamp_osm_base": "2022-11-17T17:14:39Z",
    "copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
  },
  "elements": []
}

Minimum viable example failure: https://go.dev/play/p/n7kBY3CCbHC

package main

import (
	"encoding/json"
	"fmt"

	"github.com/paulmach/osm"
)

func main() {
	var overpass osm.OSM
	buf := []byte(`{
  "version": 0.6,
  "generator": "Overpass API 0.7.59 e21c39fe",
  "osm3s": {
    "timestamp_osm_base": "2022-11-17T17:14:39Z",
    "copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL."
  },
  "elements": []
}`)
	err := json.Unmarshal(buf, &overpass)
	if err != nil {

		fmt.Println(err)
	}
}
@paulmach
Copy link
Owner

the osm website does it as a string https://www.openstreetmap.org/api/0.6/node/1.json

so I guess it needs to be interface{}

@fawick
Copy link
Author

fawick commented Nov 17, 2022

Right, good idea!

And https://github.com/paulmach/osm/blob/master/osm.go#L302 could become

o.Version = fmt.Sprint(s.Version)

Alternatively a type-switch to get around any IEEE754 weirdness

switch x:=s.Version.(type) {
case float64:
    o.Version = fmt.Sprintf("%.1f", x.Version) 
case string:   
    o.Version = x

@captchanjack
Copy link

Seeing the same issue, I used this overpass query string

"[out:json];(way[\"highway\"][\"area\"!~\"yes\"][\"highway\"!~\"cycleway|footway|path|pedestrian|steps|track|corridor|elevator|escalator|proposed|construction|bridleway|abandoned|platform|raceway|service\"][\"motor_vehicle\"!~\"no\"][\"motorcar\"!~\"no\"][\"access\"!~\"private\"][\"service\"!~\"parking|parking_aisle|driveway|private|emergency_access\"](around:500,-37.740347,144.930127);>;relation[\"restriction\"][!\"conditional\"][!\"hgv\"][\"type\"=\"restriction\"](around:500,-37.740347,144.930127);>;)->._;out body;out count;out meta;"

@paulmach
Copy link
Owner

PR to fix this #46

@paulmach
Copy link
Owner

Merged and published v0.7.1

Thank you for reporting this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants