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 design - similar API than JSON.jl #70

Closed
femtotrader opened this issue Sep 29, 2019 · 10 comments
Closed

API design - similar API than JSON.jl #70

femtotrader opened this issue Sep 29, 2019 · 10 comments
Labels

Comments

@femtotrader
Copy link

Hello,

maybe YAML.jl could mimic JSON.jl API.
Similar function naming could help to use simply YAML instead of JSON.

Kind regards

@kescobo kescobo added the API label Apr 18, 2020
@mirkobunse
Copy link
Contributor

mirkobunse commented Aug 12, 2020

I came here to suggest the same thing 👍

It's not just about the function names. It'd also be more reasonable to handle instances of Dict{AbstractString,Any} (like JSON.jl) instead of Dict{Any,Any} (like YAML currently does).

I can offer to implement these changes. I already know the package from an earlier contribution.

@mirkobunse
Copy link
Contributor

Oh, and let me point out that JSON.jl allows the user to choose the subtype of AbstractDict that will be parsed (Dict{AbstractString,Any} is only the default). First of all, this feature allows to parse into OrderedDict instances, which preserve the order of properties in the file. Second, you can alter how missing values are handled. Quote from their README:

you can pass ()->DefaultDict{String,Any}(Missing) to having any non-found keys return missing when you index the result.

You could even write your own dictionary type to define all of the behavior a JSON file should have when read! That'd be pretty cool to have here, too.

@GunnarFarneback
Copy link
Contributor

For the record, parsing YAML into ordered dicts can already be done by e.g. (this is taken from production code and not quite minimal)

function construct_ordered_mapping(constructor::YAML.Constructor,
                                   node::YAML.MappingNode; deep=false)
    YAML.flatten_mapping(node)
    mapping = OrderedDict{String, Any}()
    for (key_node, value_node) in node.value
        key = YAML.construct_object(constructor, key_node, deep)
        value = YAML.construct_object(constructor, value_node, deep)
        if !(key isa String)
            key = string(key)
        end
        mapping[key] = value
    end
    return mapping
end

constructors = Dict{String, Function}("tag:yaml.org,2002:map" =>
                                      construct_ordered_mapping)
YAML.load_file(filename, constructors)

but I would appreciate greatly if it could be done without all this overhead. In fact I would support returning OrderedDict by default, although that is a breaking change.

@mirkobunse
Copy link
Contributor

That's amazing! Looks like this could even be extended to parse arbitrary dictionary types, by making the type of mapping an argument of construct_ordered_mapping.

@mirkobunse
Copy link
Contributor

Please consider reviewing #87, where I implemented arbitrary dictionary types for YAML.load_file.

@mirkobunse
Copy link
Contributor

With the release v0.4.2 it is now possible to parse arbitrary subtypes of AbstractDict by using the new dicttype argument, which is documented in the README.

Some other advances in the direction of a JSON.jl-like API include YAML.yaml to return a YAML-formatted String object and the possibility to write arbitrary subtypes of AbstractDict.

@kescobo
Copy link
Collaborator

kescobo commented Aug 28, 2020

Are there other elements of the API we'd like to mimic, or can this be closed?

@clamydo
Copy link

clamydo commented May 17, 2021

Adding my 2 cents: Probably not what the OP meant, but unmarshalling into a type can be really powerful https://quinnj.github.io/JSON3.jl/stable/#Read-JSON-into-a-type

This concept is also used very successfully by Rust's serde crate.

@christopher-dG
Copy link
Contributor

christopher-dG commented May 17, 2021

YAML has tags to facilitate that, we could do something similar to what pyyaml does

Alternatively just using StructTypes would also be really good

@kescobo
Copy link
Collaborator

kescobo commented May 17, 2021

@clamydo Can you open a new issue? I think this specific request was closed by #87 - definitely open to additional feature requests, but let's give them their own threads so we can keep it straight.

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

No branches or pull requests

6 participants