Skip to content

Conversation

@joshbode
Copy link
Contributor

@joshbode joshbode commented Oct 2, 2016

Added _writejson method for Nullable values. Maps to JSON null if
value is null.

(edit by TotalVerb: fixes #132)

Added `_writejson` method for `Nullable` values. Maps to JSON `null` if
value is null.
joshbode added a commit to joshbode/DataFrames.jl that referenced this pull request Oct 2, 2016
- `lower` JSON method to JSONify `DataFrame`
- `readjson` method added to read JSONified `DataFrame`s

Depends upon JuliaIO/JSON.jl#174
@joshbode
Copy link
Contributor Author

joshbode commented Oct 2, 2016

Alternatively, Chars could be encoded as integers which has the nice property that they could be recovered directly via the constructor, e.g.

Char(JSON.parse(JSON.json(x::Char)))

@TotalVerb
Copy link
Collaborator

TotalVerb commented Oct 2, 2016

I think a string makes more sense for the default serialization of Char, because most JSON is to communicate with other dynamic languages like JavaScript and Python, many of which treat their characters as string types instead of numeric ones. Packages will soon be able to define their own serializations anyway, which means we only need to provide a sensible default.

Copy link
Collaborator

@TotalVerb TotalVerb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a reasonable change. @kmsquire thoughts?


# check Chars
@test json('a') == "\"a\""
@test json('\\') == "\"\\\\\""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test the following too?

  • a non-ASCII character
  • a control character (like \t)

@test sprint(JSON.print, [Inf]) == "[null]"

# check Nullables are printed correctly
@test sprint(JSON.print, [Nullable{Int64}()]) == "[null]"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Nullable() also

src/JSON.jl Outdated
end
end

function _writejson(io::IO, state::State, c::Char)
Copy link
Collaborator

@TotalVerb TotalVerb Oct 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably be made a lower method with no performance penalty (since the string has to be allocated anyway). JSON.lower(c::Char) = string(c). The Nullable one we can keep as-is, since lower would be type-unstable.

@joshbode
Copy link
Contributor Author

joshbode commented Oct 2, 2016

Thanks for the feedback :)

@joshbode
Copy link
Contributor Author

joshbode commented Oct 3, 2016

Is there anything else you need from me on this one?

@TotalVerb
Copy link
Collaborator

@joshbode I was waiting on feedback from others, but I think this will be a pretty uncontroversial change. Will merge. Do you need it tagged?

@TotalVerb TotalVerb merged commit 0e36473 into JuliaIO:master Oct 4, 2016
@joshbode
Copy link
Contributor Author

joshbode commented Oct 4, 2016

Thanks for that! Yes please :)

@TotalVerb
Copy link
Collaborator

Will do. Thank you for the contribution!

@joshbode
Copy link
Contributor Author

joshbode commented Oct 4, 2016

My pleasure - hoping to introduce JSON read/write for DataFrames and it looks like they're migrating to NullableArrays (hence me wanting Nullable to be supported :)

@TotalVerb
Copy link
Collaborator

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.

Write empty Nullable types as null

2 participants