Ysak Ysak - 4 months ago 22
JSON Question

Go JSON Naming strategy

I am very new in Go and I was exploring go to use for one of my micro services. I was wonder the way Go converts objects to Json and back to Json. But unfortunately i found configuring the output field names are little difficult with the use of tag names.

type MyStruct strust{
MyName string
}


will converts to json

{
"MyName" : "somestring"
}


But we are following a naming strategy for the whole api across Organization to follow snake_case

{
"my_name" : "somestring"
}


Is considered to be valid in my org.

I started using the tags like
json:"my_name,omitempty"
etc per field level.

I would like to know is there a way i can configure it at global project level, so that i dont want to take care this at every object and its field level.

Answer

You could try something like this: https://play.golang.org/p/i5X69ywjup

Core functionality:

// SnakeCaseEncode snake_case's the given struct's field names.
func SnakeCaseEncode(i interface{}) map[string]interface{} {
    rt, rv := reflect.TypeOf(i), reflect.ValueOf(i)

    if rt.Kind() == reflect.Ptr {
        i := reflect.Indirect(rv).Interface()
        rt, rv = reflect.TypeOf(i), reflect.ValueOf(i)
    }

    out := make(map[string]interface{}, rt.NumField())

    for i := 0; i < rt.NumField(); i++ {
        if strings.Contains(rt.Field(i).Tag.Get("json"), "omitempty") {
            continue
        }

        k := snakeCase(rt.Field(i).Name)

        out[k] = rv.Field(i).Interface()
    }

    return out
}

// snakeCase provides basic ASCII conversion of camelCase field names to snake_case.
func snakeCase(s string) string {
    out := make([]rune, 0, utf8.RuneCountInString(s))

    for i, r := range s {
        if r >= 'A' && r <= 'Z' {
            r += 32

            if i > 0 {
                out = append(out, '_')
            }
        }

        out = append(out, r)
    }

    return string(out)
}

To support map, slice etc. you'll have to expand on this simple version.

Comments