Skip to content

Commit

Permalink
add order to json, since json does not guarantee the order of inserte…
Browse files Browse the repository at this point in the history
…d keys, but it is important for xml
  • Loading branch information
kkoehn committed Oct 12, 2023
1 parent 1e0437b commit e2a4bf1
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 62 deletions.
6 changes: 3 additions & 3 deletions lib/dachsfisch/json2_xml_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def execute
def add_element(xml, element)
return unless element.is_a? Hash

element.each do |key, value|
add_node(xml, key, value) unless key.start_with?('@')
element['@@order']&.each do |key|
add_node(xml, key, element[key]) unless key.start_with?('@')
end
end

Expand All @@ -44,7 +44,7 @@ def add_node(xml, key, element)
end

def handle_attribute_and_namespaces(node, element)
element.keys.filter {|element_key| element_key.start_with?('@') }.each do |attribute_key|
element.keys.filter {|element_key| element_key.start_with?(/@[^@]/) }.each do |attribute_key|
if attribute_key.start_with? '@xmlns'
element[attribute_key].each do |namespace_key, namespace|
# add namespace of current scope to node. The root-ns($) gets 'xmlns' as key, named namespaces 'xmlns:name' respectively.
Expand Down
9 changes: 8 additions & 1 deletion lib/dachsfisch/xml2_json_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,27 @@ def execute
@fragment.elements.deconstruct.each do |root|
result[node_name(root)] = extract_node(root)
end
add_order_to_hash result
result.to_json
end

private

def add_order_to_hash(hash)
return if hash.keys.reject {|key| key.start_with?('@')}.empty?

Check warning on line 24 in lib/dachsfisch/xml2_json_converter.rb

View workflow job for this annotation

GitHub Actions / lint

[rubocop] reported by reviewdog 🐶 Space missing inside }. Raw Output: lib/dachsfisch/xml2_json_converter.rb:24:61: C: Layout/SpaceInsideBlockBraces: Space missing inside }.

hash['@@order'] = hash.keys.reject {|key| key.start_with?('@') }
end

def extract_node(node)
hash = {}
active_namespaces = add_namespaces_to_active_namespaces(node)
hash['@xmlns'] = active_namespaces unless active_namespaces.empty?

handle_attributes(hash, node)
node.children.each do |child|
handle_content(hash, child)
end
add_order_to_hash hash
hash
end

Expand Down
174 changes: 116 additions & 58 deletions spec/support/examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ def self.json
<<~JSON
{
"alice": {
"$1": "bob"
}
"$1": "bob",
"@@order": ["$1"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -31,8 +33,10 @@ def self.json
<<~JSON
{
"test": {
"$1": "test"
}
"$1": "test",
"@@order": ["$1"]
},
"@@order": ["test"]
}
JSON
end
Expand All @@ -50,12 +54,16 @@ def self.json
{
"alice": {
"bob": {
"$1": "charlie"
"$1": "charlie",
"@@order": ["$1"]
},
"david": {
"$1": "edgar"
}
}
"$1": "edgar",
"@@order": ["$1"]
},
"@@order": ["bob", "david"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -77,13 +85,17 @@ def self.json
"alice": {
"bob": [
{
"$1": "charlie"
"$1": "charlie",
"@@order": ["$1"]
},
{
"$1": "david"
"$1": "david",
"@@order": ["$1"]
}
]
}
],
"@@order": ["bob"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -104,8 +116,10 @@ def self.json
{
"alice": {
"$1": "bob",
"@charlie": "david"
}
"@charlie": "david",
"@@order": ["$1"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -125,8 +139,10 @@ def self.json
"$1": "bob",
"@xmlns": {
"$": "http://some-namespace"
}
}
},
"@@order": ["$1"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -147,8 +163,10 @@ def self.json
"@xmlns": {
"$": "http://some-namespace",
"charlie": "http://some-other-namespace"
}
}
},
"@@order": ["$1"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -170,12 +188,16 @@ def self.json
"$": "http://some-namespace"
},
"bob": {
"$1": "david"
"$1": "david",
"@@order": ["$1"]
},
"charlie:edgar": {
"$1": "frank"
}
}
"$1": "frank",
"@@order": ["$1"]
},
"@@order": ["bob", "charlie:edgar"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -197,10 +219,13 @@ def self.json
"alice": {
"$1": "bob",
"charlie": {
"$1": "bob2"
"$1": "bob2",
"@@order": ["$1"]
},
"$2": "bob3"
}
"$2": "bob3",
"@@order": ["$1", "charlie", "$2"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -217,8 +242,10 @@ def self.json
<<~JSON
{
"alice": {
"!1": "my comment"
}
"!1": "my comment",
"@@order": ["!1"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -237,8 +264,10 @@ def self.json
<<~JSON
{
"alice": {
"!1": " my comment "
}
"!1": " my comment ",
"@@order": ["!1"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -259,16 +288,21 @@ def self.json
"alice": {
"bob": [
{
"$1": "charlie"
"$1": "charlie",
"@@order": ["$1"]
},
{
"$1": "david"
"$1": "david",
"@@order": ["$1"]
},
{
"$1": "edgar"
"$1": "edgar",
"@@order": ["$1"]
}
]
}
],
"@@order": ["bob"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -291,8 +325,10 @@ def self.json
"alice": {
"bob": {
"@foo": "bar"
}
}
},
"@@order": ["bob"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -311,8 +347,10 @@ def self.json
<<~JSON
{
"alice": {
"#1": "<bob></bob>"
}
"#1": "<bob></bob>",
"@@order": ["#1"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -329,8 +367,10 @@ def self.json
<<~JSON
{
"alice": {
"#1": ""
}
"#1": "",
"@@order": ["#1"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -347,8 +387,10 @@ def self.json
<<~JSON
{
"alice": {
"#1": " "
}
"#1": " ",
"@@order": ["#1"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -366,9 +408,12 @@ def self.json
{
"alice": {
"bob": {
"$1": "charlie"
}
}
"$1": "charlie",
"@@order": ["$1"]
},
"@@order": ["bob"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -393,8 +438,10 @@ def self.json
<<~JSON
{
"alice": {
"$1": 1
}
"$1": 1,
"@@order": ["$1"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -418,9 +465,12 @@ def self.json
"@xmlns": {
"$": "http://some-other-namespace"
},
"$1": "charlie"
}
}
"$1": "charlie",
"@@order": ["$1"]
},
"@@order": ["bob"]
},
"@@order": ["alice"]
}
JSON
end
Expand All @@ -442,8 +492,10 @@ def self.json
"@xmlns": {
"bob": "http://some-other-namespace"
},
"$1": "charlie"
}
"$1": "charlie",
"@@order": ["$1"]
},
"@@order": ["bob:alice"]
}
JSON
end
Expand All @@ -460,11 +512,14 @@ def self.json
<<~JSON
{
"alice": {
"$1": "bob"
"$1": "bob",
"@@order": ["$1"]
},
"charlie": {
"$1": "david"
}
"$1": "david",
"@@order": ["$1"]
},
"@@order": ["alice", "charlie"]
}
JSON
end
Expand All @@ -485,14 +540,17 @@ def self.json
"@xmlns": {
"bob": "http://some-namespace"
},
"$1": "charlie"
"$1": "charlie",
"@@order": ["$1"]
},
"david:edgar": {
"@xmlns": {
"david": "http://some-other-namespace"
},
"$1": "foobar"
}
"$1": "foobar",
"@@order": ["$1"]
},
"@@order": ["bob:alice", "david:edgar"]
}
JSON
end
Expand Down

0 comments on commit e2a4bf1

Please sign in to comment.