#838 In/Out stream question

DanielFath Sat 28 Nov 2009

After playing with json I kinda grew tired of messing with In/Out streams so I'm looking for a quick way out. I'm sure this example is quite outdated:

input := InStream.makeForStr(str)
Str:Obj? data = Json.read(input)
data["k1"] -> "v1"

From: Json

This is as far as I've got:

str := "{\"k1\":\"v1\", \"k2\":3.4159, \"k3\":[1,2,3], \"k4\": {\"m1\":true,\"m2\":null}}"
f := File(`test.txt`)
f.writeObj(str)
data := Json.read(f.in)

So the question is since In/Out Stream have protected constructors, how are we supposed to create in/out streams that can be serialized with Json?

brian Sat 28 Nov 2009

Sorry about the docs being outdated, it should read:

str := """{"k1":"v1", "k2":3.4159, "k3":[1,2,3], "k4": {"m1":true, "m2":null}}"""
Str:Obj? data := Json.read(str.in)
data["k1"]  =>  v1

It is much easier to use a triple quote Str or a Str DSL than escaping all those quotes. And now you can use sys::Str.in to turn any string into an InStream:

fansh> using json
Add using: using json
fansh> Json.read("""{"foo":"bar"}""".in)
[foo:bar]

You can create your own subclasses of InStream/OutStream, but in general you will use these methods (especially during testing):

You can also get I/O streams from TcpSocket, UdpSocket, WebClient, WebReq, and WebRes.

I think I/O in Fantom is pretty slick b/c we boil everything down to one InStream class and one OutStream class, and we make it really easy to get an I/O stream from anything that makes sense.

DanielFath Sat 28 Nov 2009

Thanks a bunch. I didn't know each Str had its own Str.in method, I just kept looking in InStream for a method that returns Str (presumably in static context).

Login or Signup to reply.