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

Encoding to JSON produces null values #271

Closed
fatih opened this issue Jun 6, 2015 · 8 comments
Closed

Encoding to JSON produces null values #271

fatih opened this issue Jun 6, 2015 · 8 comments
Labels
feature-request A feature should be added or improved.

Comments

@fatih
Copy link

fatih commented Jun 6, 2015

Hi,

Encoding any type to JSON produces nil values to create the null value in the final output. Here is an example code to reproduce it (http://play.golang.org/p/nxETBcTjcj):

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "os"

    "github.com/aws/aws-sdk-go/aws"
)

type Image struct {
    Name        *string
    BlockDevice *string
    ImageId     *string `json:",omitempty"`
    ImageType   *string
}

func main() {
    image := &Image{
        Name:        aws.String("example"),
        BlockDevice: aws.String("/dev/sda1"),
    }

    out, err := json.MarshalIndent(&image, "", "  ")
    if err != nil {
        log.Println(err)
        os.Exit(1)
    }

    fmt.Println(string(out))
}

Output:

{
  "Name": "example",
  "BlockDevice": "/dev/sda1",
  "ImageType": null
}

As seen in the example, ImageID is not encoded because it has the omitempty struct tag, however ImageType is just null. Many types have over 20-30 fields and it causes it to create lots of null values.

Is it possible to attach the json:",omitempty" tag to the struct fields?

Thanks

@fatih
Copy link
Author

fatih commented Jun 6, 2015

Additional data:

gce: https://godoc.org/google.golang.org/api/compute/v1#ImageList
do: https://godoc.org/github.com/digitalocean/godo#Image

As seen types of other respective API's all have omitempty.

@lsegal
Copy link
Contributor

lsegal commented Jun 6, 2015

Are you looking specifically to print output structures to disk using JSON?

@fatih
Copy link
Author

fatih commented Jun 6, 2015

Yes, I want to create JSON outputs of certain types, so I can pass them to other services who consume JSON. Another use case is displaying them (think of it as a CLI), like:

aws ec2 describe-images --owners self --output json

However the types are not JSON friendly. I've checked gce and do and they both already supports it. This is btw very common in Go community and JSON is very popular so everyone would benefit from it in the long term.

@lsegal
Copy link
Contributor

lsegal commented Jun 6, 2015

@fatih I generally agree with this. We could add omitempty (and I'll keep this open to track progress), but I'm concerned that we will get similar requests like this in the future for any other supported protocol (like XML). Basically, I'm not sure this is a sustainable solution.

I really think that the encoder should ultimately be responsible for how the data is marshalled, not the struct declaration itself. For example, xml.Decoder has a few toggles to control how data is marshalled into types for the general case. I believe similar toggled could probably be added to Go's json.Encoder type (like an "OmitEmpty" flag), and I would recommend opening an issue with go to request this. This would allow the consumer to control the data formatting on their own without requiring every library to opt-in on every single data structure.

@clbanning
Copy link

@jasdel
Copy link
Contributor

jasdel commented Jun 7, 2015

@clbanning Interesting proposal, but I'd be hesitant to suggest using unsafe, because if the order of the parameters of the awsImage are changed in anyway or new parameters are added it will produced unexpected results by overlaying myImage.

http://play.golang.org/p/X-nWQhnXod // Adding an ID field produces an unexpected result.

@jasdel
Copy link
Contributor

jasdel commented Dec 1, 2015

It would be a good feature for the SDK to allow (un)marshalling JSON blobs into the SDK API types. I think the SDK would need to expose a custom JSON marshaller the SDK could use to unmarshal a JSON io.Reader or []byte to a SDK shape

@github-actions
Copy link

We have noticed this issue has not recieved attention in 3 years. We will close this issue for now. If you think this is in error, please feel free to comment and reopen the issue.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Apr 25, 2020
skotambkar added a commit to skotambkar/aws-sdk-go that referenced this issue May 20, 2021
Fixes RDS no-autopresign URL for same region issue for aws-sdk-go-v2.

Solves the issue by making sure that the presigned URLs are not created, when the source and destination regions are the same. Added and updated the tests accordingly.

Fix aws#271
skotambkar pushed a commit to skotambkar/aws-sdk-go that referenced this issue May 20, 2021
Services
===
* Synced the V2 SDK with latest AWS service API definitions.
* Fixes [aws#341](aws/aws-sdk-go-v2#341)
* Fixes [aws#342](aws/aws-sdk-go-v2#342)

SDK Breaking Changes
===
* `aws`: Add default HTTP client instead of http.DefaultClient/Transport ([aws#315](aws/aws-sdk-go-v2#315))
  * Adds a new BuildableHTTPClient type to the SDK's aws package. The type uses the builder pattern with immutable changes. Modifications to the buildable client create copies of the client.  Adds a HTTPClient interface to the aws package that the SDK will use as an abstraction over the specific HTTP client implementation. The SDK will default to the BuildableHTTPClient, but a *http.Client can be also provided for custom configuration.  When the SDK's aws.Config.HTTPClient value is a BuildableHTTPClient the SDK will be able to use API client specific request timeout options.
  * Fixes [aws#279](aws/aws-sdk-go-v2#279)
  * Fixes [aws#269](aws/aws-sdk-go-v2#269)

SDK Enhancements
===
* `service/s3/s3manager`: Update S3 Upload Multipart location ([aws#324](aws/aws-sdk-go-v2#324))
  * Updates the Location returned value of S3 Upload's Multipart UploadOutput type to be consistent with single part upload URL. This update also brings the multipart upload Location inline with the S3 object URLs created by the SDK.
  * Fixes [aws#323](aws/aws-sdk-go-v2#323)
  * V2 Port [aws#2453](aws#2453)

SDK Bugs
===
* `private/model`: Handles empty map vs unset map behavior in send request ([aws#337](aws/aws-sdk-go-v2#337))
  * Updated shape marshal model to handle the empty map vs nil map behavior. Adding a test case to assert behavior when a user sends an empty map vs nil map.
  * Fix [aws#332](aws/aws-sdk-go-v2#332)
* `service/rds`: Fix presign URL for same region ([aws#331](aws/aws-sdk-go-v2#331))
  * Fixes RDS no-autopresign URL for same region issue for aws-sdk-go-v2. Solves the issue by making sure that the presigned URLs are not created, when the source and destination regions are the same. Added and updated the tests accordingly.
  * Fix [aws#271](aws/aws-sdk-go-v2#271)
* `private/protocola/json/jsonutil`: Fix Unmarshal map[string]bool ([aws#320](aws/aws-sdk-go-v2#320))
  * Fixes the JSON unmarshaling of maps of bools. The unmarshal case was missing the condition for bool value, in addition the bool pointer.
  * Fix [aws#319](aws/aws-sdk-go-v2#319)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved.
Projects
None yet
Development

No branches or pull requests

4 participants