[alibaba/fastjson]针对Python中布尔类型 False,True 不支持解析

2024-09-02 216 views
9

解析如下格式json会失败。 { "result": False }

回答

7

解析如下格式json会失败。 { "result": False }

False改成小写(false)呢?

4

小写开头就符合Java数据类型,可以解析。与Python接口联调中发现不支持python的布尔数据类型

2

与Python接口联调中发现不支持python的布尔数据类型

按照JSON规范,你这确实是不合法的。

参考链接 :https://datatracker.ietf.org/doc/html/rfc7159.html#section-2

3.  Values

   A JSON value MUST be an object, array, number, or string, or one of
   the following three literal names:

      false null true

   The literal names MUST be lowercase.  No other literal names are
   allowed.

      value = false / null / true / object / array / number / string

      false = %x66.61.6c.73.65   ; false

      null  = %x6e.75.6c.6c      ; null

      true  = %x74.72.75.65      ; true
1

是不是应该Python侧做个转换?

"""
String to Dict (Json): json.loads(jstr)
Note: in String , shown true, in Dict shown True
Dict(Json) to String: json.dumps(jobj) 
"""    
>>> jobj = {'test': True}
>>> jstr = json.dumps(jobj)
>>> jobj
{'test': True}
>>> jstr
'{"test": true}'
>>> json.loads(jstr)
{'test': True}
5

json格式中对于boolean类型就只有 true/false,而不是 True/False

2

@harkue @convictss
同样是json规范,fastjson object key是数字的问题 (https://github.com/alibaba/fastjson/issues/3821) 求修复

https://datatracker.ietf.org/doc/html/rfc7159.html#section-4 [4]. Objects

   An object structure is represented as a pair of curly brackets
   surrounding zero or more name/value pairs (or members).  A name is a
   string.  A single colon comes after each name, separating the name
   from the value.  A single comma separates a value from a following
   name.  The names within an object SHOULD be unique.

      object = begin-object [ member *( value-separator member ) ]
               end-object

      member = string name-separator value