Skip to content

Commit

Permalink
Migrate json.kind to use enums (#30)
Browse files Browse the repository at this point in the history
Bump version
  • Loading branch information
Blacksmoke16 authored Aug 11, 2019
1 parent 5ea93de commit 45f09d5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 33 deletions.
4 changes: 2 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ name: oq
description: |
A performant, and portable jq wrapper thats facilitates the consumption and output of formats other than JSON; using jq filters to transform the data.
version: 0.2.0
version: 0.2.1

authors:
- Blacksmoke16 <[email protected]>

crystal: 0.29.0
crystal: 0.30.0

license: MIT

Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: oq
version: '0.2.0'
version: '0.2.1'
summary: A performant, and portable jq wrapper to support formats other than JSON
description: |
A performant, and portable jq wrapper thats facilitates the consumption and output of formats other than JSON; using jq filters to transform the data.
Expand Down
24 changes: 12 additions & 12 deletions src/converters/xml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module OQ::Converters::Xml

loop do
emit builder, json, xml_item: xml_item
break if json.kind == :EOF
break if json.kind.eof?
end

builder.end_element unless root.blank?
Expand All @@ -36,10 +36,10 @@ module OQ::Converters::Xml

private def self.emit(builder : XML::Builder, json : JSON::PullParser, key : String? = nil, array_key : String? = nil, *, xml_item : String) : Nil
case json.kind
when :null then json.read_null
when :string, :int, :float, :bool then builder.text get_value json
when :begin_object then handle_object builder, json, key, array_key, xml_item: xml_item
when :begin_array then handle_array builder, json, key, array_key, xml_item: xml_item
when .null? then json.read_null
when .string?, .int?, .float?, .bool? then builder.text get_value json
when .begin_object? then handle_object builder, json, key, array_key, xml_item: xml_item
when .begin_array? then handle_array builder, json, key, array_key, xml_item: xml_item
end
end

Expand All @@ -48,7 +48,7 @@ module OQ::Converters::Xml
json.read_object do |k|
if k.starts_with?('@')
builder.attribute k.lchop('@'), get_value json
elsif json.kind == :begin_array || k == "#text"
elsif json.kind.begin_array? || k == "#text"
emit builder, json, k, k, xml_item: xml_item
else
builder.element k do
Expand All @@ -62,10 +62,10 @@ module OQ::Converters::Xml
json.read_begin_array
array_key = array_key || xml_item

if json.kind == :end_array
if json.kind.end_array?
builder.element(array_key) { } unless @@at_root
else
while json.kind != :end_array
until json.kind.end_array?
builder.element array_key do
emit builder, json, key, xml_item: xml_item
end
Expand All @@ -77,10 +77,10 @@ module OQ::Converters::Xml

private def self.get_value(json : JSON::PullParser) : String
case json.kind
when :string then json.read_string
when :int then json.read_int.to_s
when :float then json.read_float.to_s
when :bool then json.read_bool.to_s
when .string? then json.read_string
when .int? then json.read_int.to_s
when .float? then json.read_float.to_s
when .bool? then json.read_bool.to_s
else
""
end
Expand Down
27 changes: 10 additions & 17 deletions src/converters/yaml.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
class JSON::Builder
def next_is_object_key?
state = @state.last
state.is_a?(ObjectState) && state.name
end
end

module OQ::Converters::Yaml
# OPTIMIZE: Figure out a way to handle aliases/anchors while streaming.
def self.deserialize(input : IO, output : IO, **args) : Nil
Expand All @@ -20,29 +13,29 @@ module OQ::Converters::Yaml
yaml.document do
loop do
case json.kind
when :null
when .null?
yaml.scalar(nil)
when :bool
when .bool?
yaml.scalar(json.bool_value)
when :int
when .int?
yaml.scalar(json.int_value)
when :float
when .float?
yaml.scalar(json.float_value)
when :string
when .string?
if YAML::Schema::Core.reserved_string?(json.string_value)
yaml.scalar(json.string_value, style: :double_quoted)
else
yaml.scalar(json.string_value)
end
when :begin_array
when .begin_array?
yaml.start_sequence
when :end_array
when .end_array?
yaml.end_sequence
when :begin_object
when .begin_object?
yaml.start_mapping
when :end_object
when .end_object?
yaml.end_mapping
when :EOF
when .eof?
break
end
json.read_next
Expand Down
2 changes: 1 addition & 1 deletion src/oq.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require "./converters/*"

# A performant and portable `jq` wrapper to support formats other than JSON.
module OQ
VERSION = "0.2.0"
VERSION = "0.2.1"

# The support formats that can be converted to/from.
enum Format
Expand Down

0 comments on commit 45f09d5

Please sign in to comment.