How does the json communication server for TCP persistent connection determine the service?

when I do a long connection service for the first time, I encounter a small problem, which used to be http. Use url to determine the service

for example, let"s be clear.
for example, there are two services, one login and one chat, and the corresponding json passed by the client is

.

Login:
{"type": "login", "username": "abc", "password": "123456"}
chat:
{"type": "chat", "from": "Zhang San", "to": "Li Si", "content": "Li Si Hello"}
the type field of json indicates which service to request

.

for golang, we usually parse these two json into corresponding structures

type Login struct{
    Type string
    Username string
    Password string
}

type Chat struct{
    Type string
    From string
    To string
    Content string
}

how can I be sure to parse the json to the corresponding structure when I have just received the json string and don"t know what type is? I should json.Unmarshal (json,?)

or was my design idea wrong in the first place?

Mar.19,2021

write your own configuration file. The structure corresponding to type is much more convenient when you add it later


Thank you for the invitation. I have been busy with something else recently and seldom come to see it.

this question is very interesting. In fact, I have encountered similar problems myself and learned from each other.

Go is a strongly typed language, so if you want to parse JSON, you are faced with two scenarios:

  • the first case: the JSON structure is clearly known.
  • the second case: the JSON structure is unknown in advance.

obviously in the first case, we can predefine the structure type to parse the JSON directly into the corresponding structure variable.

in the second case, the JSON structure is unknown and the Go language is strongly typed. We have no way to define the appropriate structure in advance, so we can use an empty interface type to parse our JSON.

We know that the interface in the go language is a very flexible feature. There is no inheritance implementation of other languages in the Go language. Polymorphism is the duck pattern: it sounds like a duck, it is a duck, and this is achieved through interface. Interface can contain a set of behavior methods. The interface is implemented as long as the type that implements these behavior methods.

since interface can contain a set of behavior methods, it can also contain nothing, so it is an empty interface. An empty interface does not need to implement any methods, and any type is an implementation of an empty interface. Conversely, interface SomeInterface {} can hold any type of value.

with the above knowledge, we can find a solution for the second case, which is to save the parsed JSON object with an empty interface type.

For more information, please see the following link: https://github.com/astaxie/bu.


  • scenario 1: find a way to define a unified structure.
  • scenario 2: first unmarshal to a structure that contains only Type attributes, and so on determine Type and then unmarshal to the target structure.
  • scenario 3: with the help of some libraries that query JSON, directly get the value of Type , and then unmarshal to the target structure. There are many libraries of this type, such as this .
Menu