duandu8892 2018-01-31 14:27
浏览 79

Go中严格的JSON解析

The encoding/json exposes a forgiving parser. Every not present property is simply set to its default value. Is there a better way to make a field required than using bulky switch statments and check every field for its default value? Another problem is that not all default types are nil. Is there another way to distinguish between than a not set field and e.g. 0 other than using pointers to be able to check against nil?

  • 写回答

1条回答 默认 最新

  • douyihuaimao733955 2018-01-31 17:37
    关注

    You may look at what there is available to implement so-called "JSON schema validation".

    You may start with this search which yields github.com/juju/gojsonschema among others; while I have no idea about its quality, it's used as part of Ubuntu's Juju cloud orchestration solution so I'd expect it to be battle tested. Still, caveat emptor.

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试