Skip to content

Commit 18e417d

Browse files
committed
Allo for extra arguments and options to the serializer interface.
1 parent c680d21 commit 18e417d

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ This gem also implements an optimized streaming writer used for generating JSON-
3232
* Each statement written as a separate node in expanded/flattened form.
3333
* `RDF List`s are written as separate nodes using `rdf:first` and `rdf:rest` properties.
3434

35-
The order of triples retrieved from the `RDF::Enumerable` dataset determines the way that JSON-LD node objects are written; for best results, statements should be ordered by _graph name_, _subect_, _predicate_ and _object_.
35+
The order of triples retrieved from the `RDF::Enumerable` dataset determines the way that JSON-LD node objects are written; for best results, statements should be ordered by _graph name_, _subject_, _predicate_ and _object_.
3636

3737
### MultiJson parser
38-
The [MultiJson](https://rubygems.org/gems/multi_json) gem is used for parsing JSON; this defaults to the native JSON parser, but will use a more performant parser if one is available. A specific parser can be specified by adding the `:adapter` option to any API call. See [MultiJson](https://rubygems.org/gems/multi_json) for more information.
38+
The [MultiJson](https://rubygems.org/gems/multi_json) gem is used for parsing and serializing JSON; this defaults to the native JSON parser/serializer, but will use a more performant parser if one is available. A specific parser can be specified by adding the `:adapter` option to any API call. Additionally, a custom serialilzer may be specified by passing the `:serializer` option to {JSON::LD::Writer} or methods of {JSON::LD::API}. See [MultiJson](https://rubygems.org/gems/multi_json) for more information.
3939

4040
### JSON-LD-star (RDFStar)
4141

Diff for: lib/json/ld/api.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def self.expand(input, framing: false, serializer: nil, **options, &block)
184184

185185
# Finally, if element is a JSON object, it is wrapped into an array.
186186
result = [result].compact unless result.is_a?(Array)
187-
result = serializer.call(result) if serializer
187+
result = serializer.call(result, **options) if serializer
188188

189189
if block_given?
190190
case block.arity
@@ -246,7 +246,7 @@ def self.compact(input, context, expanded: false, serializer: nil, **options)
246246
end
247247
result = ctx.merge(result) unless ctx.fetch('@context', {}).empty?
248248
end
249-
result = serializer.call(result) if serializer
249+
result = serializer.call(result, **options) if serializer
250250
block_given? ? yield(result) : result
251251
end
252252

@@ -330,7 +330,7 @@ def self.flatten(input, context, expanded: false, serializer: nil, **options)
330330
end
331331
end
332332

333-
flattened = serializer.call(flattened) if serializer
333+
flattened = serializer.call(flattened, **options) if serializer
334334
block_given? ? yield(flattened) : flattened
335335
end
336336

@@ -481,7 +481,7 @@ def self.frame(input, frame, expanded: false, serializer: nil, **options)
481481
result
482482
end
483483

484-
result = serializer.call(result) if serializer
484+
result = serializer.call(result, **options) if serializer
485485
block_given? ? yield(result) : result
486486
end
487487

@@ -566,7 +566,7 @@ def self.fromRdf(input, useRdfType: false, useNativeTypes: false, serializer: ni
566566
useNativeTypes: useNativeTypes)
567567
end
568568

569-
result = serializer.call(result) if serializer
569+
result = serializer.call(result, **options) if serializer
570570
block_given? ? yield(result) : result
571571
end
572572

@@ -823,7 +823,11 @@ def self.load_html(input, url:,
823823
# Defaults to `MultiJson.dump`
824824
#
825825
# @param [Object] object
826-
def self.serializer(object)
826+
# @param [Array<Object>] args
827+
# other arguments that may be passed for some specific implementation.
828+
# @param [Hash<Symbol, Object>] options
829+
# options passed from the invoking context.
830+
def self.serializer(object, *args, **options)
827831
MultiJson.dump(object, JSON_STATE)
828832
end
829833

Diff for: lib/json/ld/writer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def write_epilogue
334334
result = API.compact(result, context, **@options.merge(serializer: nil))
335335
end
336336

337-
@output.write(@serializer.call(result))
337+
@output.write(@serializer.call(result, **@options))
338338
end
339339

340340
super

0 commit comments

Comments
 (0)