Skip to content

Commit

Permalink
Merge pull request #80 from belousovAV/fix_from_json_contract
Browse files Browse the repository at this point in the history
Fix from_json contract
  • Loading branch information
rous-gg committed Aug 3, 2023
2 parents 8d8ab86 + 77d2638 commit 773cb1d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ class ReeJson::Constants
:object,
:custom,
:wab,
]
].freeze

ESCAPE_MODES = [
:newline, # allows unescaped newlines in the output.
:json, # follows the JSON specification. This is the default mode.
:xss_safe, # escapes HTML and XML characters such as & and <.
:ascii, # escapes all non-ascii or characters with the hi-bit set.
:unicode_xss, # escapes a special unicodes and is xss safe.
]
].freeze

TIME_FORMATS = []
TIME_FORMATS = [].freeze

DEFAULT_OPTIONS = {
time_format: :xmlschema,
use_as_json: true
use_as_json: true,
mode: :rails,
}.freeze
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,26 @@ class ReeJson::FromJson

fn :from_json do
link 'ree_json/constants', -> {
DEFAULT_OPTIONS & MODES & ESCAPE_MODES & TIME_FORMATS
DEFAULT_OPTIONS & MODES & ESCAPE_MODES & TIME_FORMATS
}
end

ParseJsonError = Class.new(StandardError)

contract(
Any,
Kwargs[
mode: Or[*MODES]
],
Ksplat[
mode?: Or[*MODES],
symbol_keys?: Bool,
RestKeys => Any
] => Hash
] => Any
).throws(ParseJsonError)
def call(object, mode: :rails, **opts)
def call(object, **opts)
options = DEFAULT_OPTIONS
.dup
.merge(
opts.merge(mode: mode)
)
.merge(opts)

Oj.load(object, options)
rescue ArgumentError, EncodingError
rescue ArgumentError, EncodingError, TypeError
raise ParseJsonError.new
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ class ReeJson::ToJson

contract(
Any,
Kwargs[
mode: Or[*MODES]
],
Ksplat[
mode?: Or[*MODES],
escape_mode?: Or[*ESCAPE_MODES],
float_precision?: Integer,
time_format?: Or[*TIME_FORMATS],
Expand All @@ -31,13 +29,10 @@ class ReeJson::ToJson
use_to_json?: Bool,
RestKeys => Any
] => String
).throws(ArgumentError)
def call(object, mode: :rails, **opts)
).throws(ArgumentError, TypeError)
def call(object, **opts)
options = DEFAULT_OPTIONS
.dup
.merge(
opts.merge(mode: mode)
)
.merge(opts)

Oj.dump(object, options)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,17 @@
"throws": [
"ReeJson::FromJson::ParseJsonError"
],
"return": "Hash",
"return": "Any",
"args": [
{
"arg": "object",
"arg_type": "req",
"type": "Any"
},
{
"arg": "mode",
"arg_type": "key",
"type": "Or[strict, null, compat, json, rails, object, custom, wab]"
},
{
"arg": "opts",
"arg_type": "keyrest",
"type": "Ksplat[:symbol_keys? => Bool, \"RestKeys\" => Any]"
"type": "Ksplat[:mode? => Or[strict, null, compat, json, rails, object, custom, wab], :symbol_keys? => Bool, \"RestKeys\" => Any]"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
{
"doc": "Dumps arbitrary object to json using specific dump mode.\n to_json({id: 1}) # => \"{\"id\":1}\"\n to_json({id: 1}, mode: :object) # => \"{\":id\":{\"^o\":\"Object\"}}\"\n\nList of all available Ksplat options could be found here:\nhttps://github.com/ohler55/oj/blob/develop/pages/Modes.md",
"throws": [
"ArgumentError"
"ArgumentError",
"TypeError"
],
"return": "String",
"args": [
Expand All @@ -19,15 +20,10 @@
"arg_type": "req",
"type": "Any"
},
{
"arg": "mode",
"arg_type": "key",
"type": "Or[strict, null, compat, json, rails, object, custom, wab]"
},
{
"arg": "opts",
"arg_type": "keyrest",
"type": "Ksplat[:escape_mode? => Or[newline, json, xss_safe, ascii, unicode_xss...], :float_precision? => Integer, :time_format? => Or[], :use_as_json? => Bool, :use_raw_json? => Bool, :use_to_hash? => Bool, :use_to_json? => Bool, \"RestKeys\" => Any]"
"type": "Ksplat[:mode? => Or[strict, null, compat, json, rails, object, custom, wab], :escape_mode? => Or[newline, json, xss_safe, ascii, unicode_xss...], :float_precision? => Integer, :time_format? => Or[], :use_as_json? => Bool, :use_raw_json? => Bool, :use_to_hash? => Bool, :use_to_json? => Bool, \"RestKeys\" => Any]"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,39 @@
expect(result[:id]).to be_a(Object)
}

it {
expect(from_json("null")).to eq(nil)
}

it {
expect(from_json("true")).to eq(true)
}

it {
expect(from_json('"hello"')).to eq("hello")
}

it {
expect(from_json("123")).to eq(123)
}

it {
expect(from_json("123.456")).to eq(123.456)
}

it {
expect(from_json("[1,true,\"hello\"]")).to eq([1, true, "hello"])
}

it {
expect(from_json("{\"^o\":\"Object\"}", mode: :object)).to be_a(Object)
}

it {
expect{from_json("{213: \"123\"}")}.to raise_error(ReeJson::FromJson::ParseJsonError)
}

it {
expect { from_json(nil, mode: :strict) }.to raise_error(ReeJson::FromJson::ParseJsonError)
}
end

0 comments on commit 773cb1d

Please sign in to comment.