|
74 | 74 |
|
75 | 75 | :- use_foreign_library(foreign(json)).
|
76 | 76 |
|
| 77 | +:- multifile |
| 78 | + json_write_hook/4. % +Term, +Stream, +State, +Options |
| 79 | + |
77 | 80 | :- predicate_options(json_read/3, 3,
|
78 | 81 | [ null(ground),
|
79 | 82 | true(ground),
|
|
535 | 538 | % objects can be safely embedded into an HTML =|<script>|=
|
536 | 539 | % element.
|
537 | 540 |
|
| 541 | +%! json_write_hook(+Term, +Stream, +State, +Options) is semidet. |
| 542 | +% |
| 543 | +% Hook that can be used to emit a JSON representation for Term to |
| 544 | +% Stream. If the predicate succeeds it __must__ have written a |
| 545 | +% __valid__ JSON data element and if it fails it may not have produced |
| 546 | +% any output. This facility may be used to map arbitrary Prolog terms |
| 547 | +% to JSON. It was added to manage the precision with which floating |
| 548 | +% point numbers are emitted. |
| 549 | +% |
| 550 | +% Note that this hook is shared by all users of this library. It is |
| 551 | +% generally adviced to map a unique compound term to avoid |
| 552 | +% interference with normal output. |
| 553 | +% |
| 554 | +% @arg State and Options are opaque handles to the current output |
| 555 | +% state and settings. Future versions may provide documented access |
| 556 | +% to these terms. Currently it is adviced to ignore these arguments. |
| 557 | + |
| 558 | + |
| 559 | + |
538 | 560 | :- record json_write_state(indent:nonneg = 0,
|
539 | 561 | step:positive_integer = 2,
|
540 | 562 | tab:positive_integer = 8,
|
|
588 | 610 | indent(Stream, State),
|
589 | 611 | write(Stream, ']')
|
590 | 612 | ).
|
| 613 | + |
| 614 | +json_write_term(Term, Stream, State, Options) :- |
| 615 | + json_write_hook(Term, Stream, State, Options), |
| 616 | + !. |
591 | 617 | json_write_term(Number, Stream, _State, _Options) :-
|
592 | 618 | number(Number),
|
593 | 619 | !,
|
|
0 commit comments