SoFunction
Updated on 2025-03-05

Detailed explanation of how Go language parses annotated json

1. Background and significance

The standard json format is not annotated, but sometimes in order to facilitate understanding of the meaning of each field in json, annotated json is needed. For example, when yapi, a commonly used interface document management platform, supports configuring annotated json when configuring interface parameters and interface return, or creating mock data. This article gives an example of parsing annotated jsons in Go language.

2. Go language analysis ordinary json

The official json library of go language can parse ordinary jsons. For example, we create a file with the following code:

package main

import (
   "encoding/json"
   "fmt"
)

type user struct {
   Name    string
   Age     int
   Address struct {
      City    string
      Country string
   }
}

func main() {
   jsonStr := `{
        "name": "ZhangSan",
        "age": 11,
        "address": {
          "city": "Shanghai",
          "country": "China"
        }
      }`
   user := &user{}
   err := ([]byte(jsonStr), user)
   if err != nil {
      ("json parsing failed: %+v", err)
   } else {
      ("json parsing result: %+v", user)
   }
}

rungo run The command, the running result is:

json analysis result: &{Name:ZhangSan Age:11 Address:{City:Shanghai Country:China}}

But if we add in json// ... or/* ... */Format comments:

package main

import (
   "encoding/json"
   "fmt"
)

type user struct {
   Name    string
   Age     int
   Address struct {
      City    string
      Country string
   }
}

func main() {
   jsonStr := `{
        "name": "ZhangSan",    // Name        "age": 11,             // age        /* Address Information Start */
        "address": {
          "city": "Shanghai",
          "country": "China"
        }
        /* Address Information End */
      }`
   user := &user{}
   err := ([]byte(jsonStr), user)
   if err != nil {
      ("json parsing failed: %+v", err)
   } else {
      ("json parsing result: %+v", user)
   }
}

Then execute the commandgo run , you can see that the running result is:

json parsing failed: invalid character '/' looking for beginning of object key string

3. Go language parsing with comments json

use/titanous/json5The library can parse commented json

package main

import (
   "fmt"
   "/titanous/json5"
)

type user struct {
   Name     string
   Age      int
   HomePage string
   Address  struct {
      City    string
      Country string
   }
}

func main() {
   jsonStr := `{
        "name": "ZhangSan",    // Name        "age": 11,             // age        /* Address Information Start */
        "address": {
          "city": "Shanghai",
          "country": "China"
        }
        /* Address Information End */
      }`
   user := &user{}
   err := ([]byte(jsonStr), user)
   if err != nil {
      ("json parsing failed: %+v", err)
   } else {
      ("json parsing result: %+v", user)
   }
}

The operation results are as follows:

json analysis result: &{Name:ZhangSan Age:11 HomePage: Address:{City:Shanghai Country:China}}

4. Verification of other special circumstances

In theory, in theory,// ...and/* ... */If it appears in a string, then they should not be processed for comments. Let's verify/titanous/json5Is it possible to correct if this situation, modify the file to:

package main

import (
   "fmt"
   "/titanous/json5"
)

type user struct {
   Name     string
   Age      int
   HomePage string
   Comment  string
   Address  struct {
      City    string
      Country string
   }
}

func main() {
   jsonStr := `{
        "name": "ZhangSan",    // Name        "age": 11,             // age
        /* There is "//" in the value of the homePage field, but it should not be processed as annotation */
        "homePage": "/",
        "comment": "Test it here, /* Can the comments here be parsed correctly */",

        /* Address Information Start */
        "address": {
          "city": "Shanghai",
          "country": "China"
        }
        /* Address Information End */
      }`
   user := &user{}
   err := ([]byte(jsonStr), user)
   if err != nil {
      ("json parsing failed: %+v", err)
   } else {
      ("json parsing result: %+v", user)
   }
}

The running result is:

json analysis result: &{Name:ZhangSan Age:11 HomePage:/ Comment: Let's test it here, /* Can the comments here be correctly parsed */ Address:{City:Shanghai Country:China}}

You can see,/titanous/json5The handling of this special situation is also completely correct.

This is the end of this article about how to analyze annotated json in Go language. For more related Go analysis annotated json content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!