0

I can read JSON from a file into a string and then deserialize like so:

val jsonString = new String(Files.readAllBytes(Paths.get("myObject.json")))
val json = parse(jsonString)
val myObject = json.extract[MyClass]

This seems inefficient to me, especially for large files, because I'm reading into a string first and only then passing the string to the deserializer.

Is it possible to deserialize directly from a file using json4s, or is the only way to read into memory first?

arosca
  • 469
  • 7
  • 15

1 Answers1

0

I don't see how you could: you don't know that a json message is well-formed until you have reached its end.

John K
  • 1,285
  • 6
  • 18
  • That's a good point but I'm not sure it's an impediment. If you encounter an unexpected token at some point you can always raise an exception. – arosca Aug 26 '16 at 01:28
  • I thought about this a bit more; even if the string is in memory parsing probably still works the same way (left to right) and you cannot determine if the JSON is well-formed without reading the string sequentially to the end. So I see no difference between parsing directly from disk vs. reading the entire string in memory first. I'll probably dig through the source code a bit and see if it's possible to modify it easily to read directly from disk. – arosca Aug 29 '16 at 13:14