quicktype icon indicating copy to clipboard operation
quicktype copied to clipboard

Go language support JSON Schema binary data to byte array type

Open ryan0x44 opened this issue 1 year ago • 0 comments

Context (Input, Language)

If you have binary data (e.g. TypeScript ArrayBuffer) and you want to store it in a JSON document, the recommended JSON Schema is:

{
	"type": "string",
	"contentEncoding": "base64",
	"contentMediaType": "application/octet-stream"
}

(this is per https://json-schema.org/understanding-json-schema/reference/non_json_data )

In Go, using the above JSON Schema with quicktype currently generates code using the string type, where []byte is actually correct because json Marshal/Unmarshal will correctly base64 encode/decode the payload into the byte array (where as string will leave the base64 string as-is).

Description

I'm trying to interop between a TypeScript component and a Go component, however there is no way to get Go generated code to use a byte array type []byte where I need it.

Current Behaviour / Output

Current Go type is string

Proposed Behaviour / Output

Desired Go type is []byte

Solution

Solution would be for JSON Schema fields which have a string type, to check if contentEncoding and contentMediaType is defined as above, and then use the []byte type.

ryan0x44 avatar Sep 09 '24 07:09 ryan0x44