dongzhang3482 2018-06-06 11:32
浏览 420


So, my use case consists of parsing varying JSON schemas into new struct types, which will be further used with an ORM to fetch data from a SQL database. Being compiled in nature, I believe there will not be an out-of-the-box solution in go, but is there any hack available to do this, without creating a separate go process. I tried with reflection, but could not find a satisfactory approach.

Currently, I am using a-h generate library which does generate the structs, but I am stuck at how to load these new struct types in go runtime.


Example JSON Schema:

  "$schema": "",
  "title": "Address",
  "id": "Address",
  "type": "object",
  "description": "address",
  "properties": {
    "houseName": {
      "type": "string",
      "description": "House Name",
      "maxLength": 30
    "houseNumber": {
      "type": "string",
      "description": "House Number",
      "maxLength": 4
    "flatNumber": {
      "type": "string",
      "description": "Flat",
      "maxLength": 15
    "street": {
      "type": "string",
      "description": "Address 1",
      "maxLength": 40
    "district": {
      "type": "string",
      "description": "Address 2",
      "maxLength": 30
    "town": {
      "type": "string",
      "description": "City",
      "maxLength": 20
    "county": {
      "type": "string",
      "description": "County",
      "maxLength": 20
    "postcode": {
      "type": "string",
      "description": "Postcode",
      "maxLength": 8

Now, in the above-mentioned library, there is a command line tool, which generates the text for struct type for above json as below:

// Code generated by schema-generate. DO NOT EDIT.

package main

// Address address
type Address struct {
  County string `json:"county,omitempty"`
  District string `json:"district,omitempty"`
  FlatNumber string `json:"flatNumber,omitempty"`
  HouseName string `json:"houseName,omitempty"`
  HouseNumber string `json:"houseNumber,omitempty"`
  Postcode string `json:"postcode,omitempty"`
  Street string `json:"street,omitempty"`
  Town string `json:"town,omitempty"`

Now, the issue is that how to use this struct type without re-compilation in the program. There is a hack, where I can start a new go process, but that doesn't seem a good way to do it. One other way is to write my own parser for unmarshalling JSON schema, something like:

b := []byte(`{"Name":"Wednesday","Age":6,"Parents":["Gomez","Morticia"]}`)
var f interface{}
json.Unmarshal(b, &f)
m := f.(map[string]interface{})
for k, v := range m {
    switch vv := v.(type) {
    case string:
        fmt.Println(k, "is string", vv)
    case float64:
        fmt.Println(k, "is float64", vv)
    case int:
        fmt.Println(k, "is int", vv)
    case []interface{}:
        fmt.Println(k, "is an array:")
        for i, u := range vv {
            fmt.Println(i, u)
        fmt.Println(k, "is of a type I don't know how to handle")

Can someone please suggest some pointers to look for. Thanks.

  • 写回答



      相关推荐 更多相似问题


      • ¥20 苹果app名字侵权商标,我投诉了,但是苹果一直没回怎么办
      • ¥15 Android studio运行不成功总是报错
      • ¥15 pycharm 加载软件包列表时出错connect timed out
      • ¥15 和您碰到了同一个问题
      • ¥15 如何在微信支付V3PHP版请求中添加header?
      • ¥15 在数字信号处理中用MATLAB显示波形。
      • ¥15 keil MDK软件进行仿真调试的局限
      • ¥15 关于gateway产生临时文件夹的问题
      • ¥15 Java Broken Pipe以及OOM
      • ¥20 请问有没有RTL8211FS的参考原理图