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

Marshal Empty Properties as Empty Object #22

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ func ExampleFeatureCollection_MarshalJSON() {
}

fmt.Printf("%s", string(rawJSON))
// Output: {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":null}]}
// Output: {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1,2]},"properties":{}}]}
}
2 changes: 2 additions & 0 deletions feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func (f Feature) MarshalJSON() ([]byte, error) {
}
if f.Properties != nil && len(f.Properties) != 0 {
fea.Properties = f.Properties
} else {
fea.Properties = make(map[string]interface{})
}
if f.CRS != nil && len(f.CRS) != 0 {
fea.CRS = f.CRS
Expand Down
16 changes: 8 additions & 8 deletions feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestFeatureMarshalJSON(t *testing.T) {
t.Fatalf("should marshal to json just fine but got %v", err)
}

if !bytes.Contains(blob, []byte(`"properties":null`)) {
t.Errorf("json should set properties to null if there are none")
if !bytes.Contains(blob, []byte(`"properties":{}`)) {
t.Errorf("json should set properties to empty object if there are none")
}
}

Expand All @@ -35,8 +35,8 @@ func TestFeatureMarshal(t *testing.T) {
t.Fatalf("should marshal to json just fine but got %v", err)
}

if !bytes.Contains(blob, []byte(`"properties":null`)) {
t.Errorf("json should set properties to null if there are none")
if !bytes.Contains(blob, []byte(`"properties":{}`)) {
t.Errorf("json should set properties to empty object if there are none")
}
}

Expand All @@ -48,8 +48,8 @@ func TestFeatureMarshalValue(t *testing.T) {
t.Fatalf("should marshal to json just fine but got %v", err)
}

if !bytes.Contains(blob, []byte(`"properties":null`)) {
t.Errorf("json should set properties to null if there are none")
if !bytes.Contains(blob, []byte(`"properties":{}`)) {
t.Errorf("json should set properties to empty object if there are none")
}
}

Expand Down Expand Up @@ -89,7 +89,7 @@ func TestMarshalFeatureID(t *testing.T) {
t.Fatalf("should marshal, %v", err)
}

if !bytes.Equal(data, []byte(`{"id":"asdf","type":"Feature","geometry":null,"properties":null}`)) {
if !bytes.Equal(data, []byte(`{"id":"asdf","type":"Feature","geometry":null,"properties":{}}`)) {
t.Errorf("data not correct")
t.Logf("%v", string(data))
}
Expand All @@ -101,7 +101,7 @@ func TestMarshalFeatureID(t *testing.T) {

}

if !bytes.Equal(data, []byte(`{"id":123,"type":"Feature","geometry":null,"properties":null}`)) {
if !bytes.Equal(data, []byte(`{"id":123,"type":"Feature","geometry":null,"properties":{}}`)) {
t.Errorf("data not correct")
t.Logf("%v", string(data))
}
Expand Down