Skip to content
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

API: Improve MarshalToJson() in common/reflect/marshal.go #3655

Merged
merged 5 commits into from
Aug 8, 2024

Conversation

vrnobody
Copy link
Contributor

@vrnobody vrnobody commented Aug 7, 2024

这个 PR 对 common/reflect/marshal.go 里面的 MarshalToJson() 做了以下优化了:

  • enum 由原来的显示数字改为显示变量名
  • 设置了 json 标签的 struct 字段,改为显示 json 标签内容
  • 不显示空值、空字符串 或者 设置了 json:"omitempty" 的零值
  • IP 地址由原来的显示数组 [127, 0, 0, 1] 改为显示字符串 "127.0.0.1"
  • 端口列表由原来的显示结构体改为显示字符串
  • 添加参数控制是否显示 _TypedMessage_

一个简单的样例:

{
  "account": {
    "cipherType": "CHACHA20_POLY1305",
    "password": "shadowsocks-password"
  },
  "email": "[email protected]"
}

之前是这样:

{
    "Account": {
        "CipherType": 7,
        "IvCheck": false,
        "Password": "shadowsocks-password",
        "_TypedMessage_": "xray.proxy.shadowsocks.Account"
    },
    "Email": "[email protected]",
    "Level": 0
}
更详细的样例
config.json ./xray.exe -c config.json -dump
{
  "log": {
    "loglevel": "debug"
  },
  "inbounds": [
    {
      "tag": "agentin",
      "protocol": "http",
      "port": 8080,
      "listen": "127.0.0.1",
      "settings": {}
    }
  ],
  "outbounds": [
    {
      "tag": "ssout",
      "protocol": "shadowsocks",
      "settings": {
        "servers": [
          {
            "address": "bing.com",
            "port": 443,
            "method": "chacha20-ietf-poly1305",
            "password": "1234",
            "ota": false,
            "level": 0
          }
        ]
      }
    }
  ]
}

{
  "inbounds": [
    {
      "listen": "127.0.0.1",
      "port": 8080,
      "protocol": "http",
      "settings": {},
      "tag": "agentin"
    }
  ],
  "log": {
    "dnsLog": false,
    "loglevel": "debug"
  },
  "outbounds": [
    {
      "protocol": "shadowsocks",
      "settings": {
        "servers": [
          {
            "address": "bing.com",
            "port": 443,
            "method": "chacha20-ietf-poly1305",
            "password": "1234",
            "ota": false,
            "level": 0
          }
        ]
      },
      "tag": "ssout"
    }
  ],
  "port": 0
}

@yuhan6665
Copy link
Member

感谢佬 这个很实用 我准备把现有的 protojson 都改成你这个 MarshalToJson

@yuhan6665 yuhan6665 changed the title Improve MarshalToJson() in common/reflect/marshal.go API: Improve MarshalToJson() in common/reflect/marshal.go Aug 8, 2024
@yuhan6665 yuhan6665 merged commit ac628a9 into XTLS:main Aug 8, 2024
36 checks passed
@vrnobody
Copy link
Contributor Author

vrnobody commented Aug 8, 2024

不要换成这个。reflection有一定的危险性,能用标准库的地方还是用标准库吧。

@vrnobody vrnobody deleted the creflect branch August 9, 2024 00:06
@yuhan6665
Copy link
Member

就只有 API proto 转 json 这块用(非核心功能)应该还好?

@vrnobody
Copy link
Contributor Author

vrnobody commented Aug 9, 2024

那块可以。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants