在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:quinnj/JSON2.jl开源软件地址:https://github.com/quinnj/JSON2.jl开源编程语言:Julia 100.0%开源软件介绍:JSON2Fast JSON for Julia types
This package is deprecated. Its successor is at JSON3.jl. InstallationThe package is registered in julia> Pkg.add("JSON2") Project StatusThe package is tested against the current Julia Contributing and QuestionsContributions are very welcome, as are feature requests and suggestions. Please open an issue if you encounter any problems or would just like to ask a question. DocumentationFor most use-cases, all you ever need are: JSON2.write(obj) => String
JSON2.read(str, T) => T
@pretty json_string # print a "prettified" version of a JSON string Native support for reading/writing is provided for:
Custom types are supported by default as well, utilizing reflection to generate compiled JSON parsers for a type's fields. So in general, you really can just do Custom JSON FormattingDefaultIn many cases, a type doesn't even need to use struct T
a::Int
b::Int
c::Union{Nothing, Int}
end Could have valid JSON in the forms: {"a": 0, "b": 1, "c": null} // all 3 fields provided in correct order
{"a": 0, "b": 1, "c": 2}
{"a": 0, "b": 1, "c": null, "d": 3} // extra fields are ignored
{"a": 0} // will work if T(a) constructor is defined
{"a": 0, "b": 1} // will work if T(a, b) constructor is defined That is, each field must be present in the JSON input and match in position to the original struct definition. Extra arguments after the struct's own fieldtypes are ignored. As noted, the exception to a field needing to be present is if 1) the field and all subsequent fields are not present and 2) appropriate constructors are defined that take these limited subsets of inputs when constructing, e.g. JSON.@format TJSON2.@format T [noargs|keywordargs] begin
_field_ => (; options...)
_field2_ => (; options...)
end Specify a custom JSON formatting for a struct
Again, the default case is for JSON input that will have consistently ordered, always-present fields; for cases where the input JSON is not well-ordered or if there is a possibility of a field not being present in the JSON input, there are a few additional options for custom parsing. Default field valuesIf the JSON input fields will always be consistenly-ordered, but fields may be missing (i.e. field isn't present at all in the input), field defaults can be provided like: JSON2.@format T begin
c => (default=0,)
end This says that, when reading from a JSON input, if field If the JSON input is not consistenly-ordered, there are two other options for allowing direct type parsing Keywordargs ConstructorT(; a=0, b=0, c=0, kwargs...) = T(a, b, c)
JSON2.@format T keywordargs begin
# ...
end Here we've defined a "keywordargs" constructor for Noargs Constructormutable struct T
a::Int
b::Int
c::Union{Nothing, Int}
end
T() = T(0, 0, 0)
JSON2.@format T noargs begin
#...
end In this case, we've made |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论