Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /smlnj-lib/trunk/JSON/json-printer.sml
ViewVC logotype

Annotation of /smlnj-lib/trunk/JSON/json-printer.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2996 - (view) (download)

1 : jhr 2996 (* json-printer.sml
2 :     *
3 :     * COPYRIGHT (c) 2008 The Fellowship of SML/NJ (http://www.smlnj.org)
4 :     * All rights reserved.
5 :     *
6 :     * A printer for JSON values.
7 :     *)
8 :    
9 :     structure JSONPrinter : sig
10 :    
11 :     val print : TextIO.outstream * JSON.value -> unit
12 :     val print' : {strm : TextIO.outstream, pretty : bool} -> JSON.value -> unit
13 :    
14 :     end = struct
15 :    
16 :     structure J = JSON
17 :     structure JSP = JSONStreamPrinter
18 :    
19 :     fun printWith printer = let
20 :     fun pr (J.OBJECT fields) = let
21 :     fun prField (key, v) = (JSP.objectKey(printer, key); pr v)
22 :     in
23 :     JSP.beginObject printer;
24 :     List.app prField fields;
25 :     JSP.endObject printer
26 :     end
27 :     | pr (J.ARRAY vs) = (
28 :     JSP.beginArray printer;
29 :     List.app pr vs;
30 :     JSP.endArray printer)
31 :     | pr J.NULL = JSP.null printer
32 :     | pr (J.BOOL b) = JSP.boolean (printer, b)
33 :     | pr (J.INT n) = JSP.integer (printer, n)
34 :     | pr (J.FLOAT f) = JSP.float (printer, f)
35 :     | pr (J.STRING s) = JSP.string (printer, s)
36 :     in
37 :     fn v => (pr v; JSP.close printer)
38 :     end
39 :    
40 :     fun print (strm, v) = printWith (JSP.new strm) v
41 :    
42 :     fun print' {strm, pretty} = printWith (JSP.new' {strm=strm, pretty=pretty})
43 :    
44 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0