Changeset 29173 in project


Ignore:
Timestamp:
06/25/13 11:20:34 (8 years ago)
Author:
Moritz Heidkamp
Message:

medea: Add some examples to documentation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/medea

    r29172 r29173  
    7676
    7777
     78=== Examples
     79
     80<enscript language="scheme">
     81(use medea)
     82
     83;; Reading from a port
     84(with-input-from-string "[1,2,3]" read-json) ; => #(1 2 3)
     85
     86
     87;; Reading from a string
     88(read-json "[1,2,3]") ; => #(1 2 3)
     89
     90
     91;; Writing to a port
     92(write-json '((foo . 1) (bar . 2))) ; output: {"foo":1,"bar":2}
     93
     94
     95;; Writing to a string
     96(json->string '((foo . "bar"))) ; => "{\"foo\":\"bar\"}"
     97
     98
     99;; Parsing JSON arrays as lists instead of vectors
     100(define array-as-list-parser
     101  (cons 'array (lambda (x) x)))
     102
     103(json-parsers (cons array-as-list-parser (json-parsers)))
     104
     105(read-json "[1,2,3]") ; => (1 2 3)
     106
     107 
     108;; Adding support for writing u8vectors
     109(use srfi-4)
     110
     111(define (u8vector-unparser v)
     112  (write-json (blob->string (u8vector->blob/shared v))))
     113
     114(json-unparsers (cons (cons u8vector? u8vector-unparser) (json-unparsers)))
     115
     116(write-json '((nice . #u8(99 104 105 99 107 101 110 33)))) ; output: {"nice":"chicken!"}
     117</enscript>
     118
     119
    78120=== About this egg
    79121
Note: See TracChangeset for help on using the changeset viewer.