Skip to content

proposal: encoding/json: add schema support #62229

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

Closed
suiriass opened this issue Aug 23, 2023 · 3 comments
Closed

proposal: encoding/json: add schema support #62229

suiriass opened this issue Aug 23, 2023 · 3 comments

Comments

@suiriass
Copy link

Background

Currently, the encoding/json package in Go provides functionality for encoding and decoding JSON data, but it does not support JSON Schema. JSON Schema is a powerful tool for validating the structure of JSON data, and it's widely used in many applications.

Proposal

I propose to add JSON Schema support to the encoding/json package. This would allow developers to validate JSON data against a schema, and it could also provide a way to automatically generate Go types from a JSON Schema.

Benefits

Adding JSON Schema support would have several benefits:

  1. Data validation: JSON Schema provides a clear and standardized way to validate JSON data. This can help prevent bugs and improve the reliability of applications.

  2. Code generation: With JSON Schema support, we could potentially generate Go types from a schema. This could save developers time and reduce the risk of errors.

  3. Interoperability: JSON Schema is a widely used standard. By supporting it in Go, we can improve the interoperability of Go applications with other systems.

Implementation

The implementation details would need to be discussed further. One possible approach could be to integrate an existing open-source JSON Schema library into the encoding/json package

The new functionality could look something like this:

// Load a JSON Schema from a file
schema, err := json.LoadSchema("schema.json")
if err != nil {
    // handle error
}

// Validate JSON data against the schema
err = schema.Validate(jsonData)
if err != nil {
    // handle validation error
}

// Unmarshal Go types from the schema
types, err := schema.Unmarshal(jsonData)
if err != nil {
    // handle error
}

This would make it much easier to work with JSON data in Go, especially in applications that need to handle complex or variable data structures.

Impact
Adding JSON Schema support to the encoding/json package would increase its complexity and maintenance cost. However, I believe the benefits would outweigh the costs. JSON Schema is a widely used standard, and supporting it in Go would make the language more attractive to developers who work with JSON data.

I'm looking forward to hearing your thoughts on this proposal.

Thank you.

@gopherbot gopherbot added this to the Proposal milestone Aug 23, 2023
@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Aug 23, 2023
@ianlancetaylor ianlancetaylor changed the title proposal: Add JSON Schema Support to encoding/json proposal: encoding/json: add schema support Aug 23, 2023
@ianlancetaylor
Copy link
Member

CC @dsnet @mvdan

@dsnet
Copy link
Member

dsnet commented Aug 23, 2023

Hi, thanks for your proposal.

It's not clear to me that this is something that needs to live inside the standard library, as opposed to something that is first developed externally and once it has proven it's utility and worth, then be considered for adoption. In your example, all of the schema parsing and validation logic occurs separate from the unmarshal, which calls into question whether this needs to be part of the "json" package versus say a "jsonschema" package.

Also, I think it is highly unlikely to adopt something in the stdlib where the specification is not finalized: https://json-schema.org/specification.html

@suiriass
Copy link
Author

Ok, thanks for the answer, I'll look for another way。

@golang golang locked and limited conversation to collaborators Aug 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants