lib/json/ld/compact.rb in json-ld-3.0.0 vs lib/json/ld/compact.rb in json-ld-3.0.1
- old
+ new
@@ -7,12 +7,14 @@
##
# This algorithm compacts a JSON-LD document, such that the given context is applied. This must result in shortening any applicable IRIs to terms or compact IRIs, any applicable keywords to keyword aliases, and any applicable JSON-LD values expressed in expanded form to simple values such as strings or numbers.
#
# @param [Array, Hash] element
# @param [String] property (nil)
+ # @param [Boolean] ordered (true)
+ # Ensure output objects have keys ordered properly
# @return [Array, Hash]
- def compact(element, property: nil)
+ def compact(element, property: nil, ordered: false)
#if property.nil?
# log_debug("compact") {"element: #{element.inspect}, ec: #{context.inspect}"}
#else
# log_debug("compact") {"property: #{property.inspect}"}
#end
@@ -23,11 +25,11 @@
self.context = (td && td.context && self.context.parse(td.context)) || input_context
case element
when Array
#log_debug("") {"Array #{element.inspect}"}
- result = element.map {|item| compact(item, property: property)}.compact
+ result = element.map {|item| compact(item, property: property, ordered: ordered)}.compact
# If element has a single member and the active property has no
# @container mapping to @list or @set, the compacted value is that
# member; otherwise the compacted value is element
if result.length == 1 &&
@@ -52,11 +54,11 @@
end
end
# If expanded property is @list and we're contained within a list container, recursively compact this item to an array
if list?(element) && context.container(property) == %w(@list)
- return compact(element['@list'], property: property)
+ return compact(element['@list'], property: property, ordered: ordered)
end
inside_reverse = property == '@reverse'
result, nest_result = {}, nil
@@ -69,11 +71,11 @@
each do |term|
term_context = self.context.term_definitions[term].context if context.term_definitions[term]
self.context = context.parse(term_context) if term_context
end
- element.keys.sort.each do |expanded_property|
+ element.keys.opt_sort(ordered: ordered).each do |expanded_property|
expanded_value = element[expanded_property]
#log_debug("") {"#{expanded_property}: #{expanded_value.inspect}"}
if expanded_property == '@id' || expanded_property == '@type'
compacted_value = Array(expanded_value).map do |expanded_type|
@@ -87,11 +89,11 @@
result[al] = compacted_value
next
end
if expanded_property == '@reverse'
- compacted_value = compact(expanded_value, property: '@reverse')
+ compacted_value = compact(expanded_value, property: '@reverse', ordered: ordered)
#log_debug("@reverse") {"compacted_value: #{compacted_value.inspect}"}
# handle double-reversed properties
compacted_value.each do |prop, value|
if context.reverse?(prop)
add_value(result, prop, value,
@@ -108,11 +110,11 @@
next
end
if expanded_property == '@preserve'
# Compact using `property`
- compacted_value = compact(expanded_value, property: property)
+ compacted_value = compact(expanded_value, property: property, ordered: ordered)
#log_debug("@preserve") {"compacted_value: #{compacted_value.inspect}"}
unless compacted_value.is_a?(Array) && compacted_value.empty?
result['@preserve'] = compacted_value
end
@@ -174,11 +176,11 @@
when list?(expanded_item) then expanded_item['@list']
when graph?(expanded_item) then expanded_item['@graph']
else expanded_item
end
- compacted_item = compact(value, property: item_active_property)
+ compacted_item = compact(value, property: item_active_property, ordered: ordered)
#log_debug("") {" => compacted key: #{item_active_property.inspect} for #{compacted_item.inspect}"}
# handle @list
if list?(expanded_item)
compacted_item = as_array(compacted_item)
@@ -265,11 +267,10 @@
property_is_array: as_array)
end
end
end
- # Re-order result keys
- result.keys.sort.each_with_object({}) {|kk, memo| memo[kk] = result[kk]}
+ result
else
# For other types, the compacted value is the element value
#log_debug("compact") {element.class.to_s}
element
end