== Record Parser

The record parser allows you to easily parse protocols which are delimited by a sequence of bytes, or fixed
size records.

It transforms a sequence of input buffer to a sequence of buffer structured as configured (either
fixed size or separated records).

For example, if you have a simple ASCII text protocol delimited by '\n' and the input is the following:

[source]
----
buffer1:HELLO\nHOW ARE Y
buffer2:OU?\nI AM
buffer3: DOING OK
buffer4:\n
----

The record parser would produce
[source]
----
buffer1:HELLO
buffer2:HOW ARE YOU?
buffer3:I AM DOING OK
----

Let's see the associated code:

[source, ceylon]
----
import io.vertx.ceylon.core.buffer { buffer } 
import io.vertx.ceylon.core.parsetools { recordParser } 

...

value parser = recordParser.newDelimited("\n", (Buffer h) {
  print(h.toString());
});

parser.handle(buffer.buffer("HELLO\nHOW ARE Y"));
parser.handle(buffer.buffer("OU?\nI AM"));
parser.handle(buffer.buffer("DOING OK"));
parser.handle(buffer.buffer("\n"));

----

You can also produce fixed sized chunks as follows:

[source, ceylon]
----
import io.vertx.ceylon.core.parsetools { recordParser } 

...

recordParser.newFixed(4, (Buffer h) {
  print(h.toString());
});

----

For more details, check out the `link:../../ceylondoc/vertx-core//parsetools/RecordParser.type.html[RecordParser]` class.

== Json Parser

You can easily parse JSON structures but that requires to provide the JSON content at once, but it
may not be convenient when you need to parse very large structures.

The non-blocking JSON parser is an event driven parser able to deal with very large structures.
It transforms a sequence of input buffer to a sequence of JSON parse events.

[source, ceylon]
----
Code not translatable : Unsupported kind ENUM_CONSTANT
----

The parser is non-blocking and emitted events are driven by the input buffers.

[source, ceylon]
----
import io.vertx.ceylon.core.buffer { buffer } 
import io.vertx.ceylon.core.parsetools { jsonParser } 

...


value parser = jsonParser.newParser();

// start array event
// start object event
// "firstName":"Bob" event
parser.handle(buffer.buffer("[{\"firstName\":\"Bob\","));

// "lastName":"Morane" event
// end object event
parser.handle(buffer.buffer("\"lastName\":\"Morane\"},"));

// start object event
// "firstName":"Luke" event
// "lastName":"Lucky" event
// end object event
parser.handle(buffer.buffer("{\"firstName\":\"Luke\",\"lastName\":\"Lucky\"}"));

// end array event
parser.handle(buffer.buffer("]"));

// Always call end
parser.end();

----

Event driven parsing provides more control but comes at the price of dealing with fine grained events, which can be
inconvenient sometimes. The JSON parser allows you to handle JSON structures as values when it is desired:

[source, ceylon]
----
Code not translatable : Unsupported kind ENUM_CONSTANT
----

The value mode can be set and unset during the parsing allowing you to switch between fine grained
events or JSON object value events.

[source, ceylon]
----
Code not translatable : Unsupported kind ENUM_CONSTANT
----

You can do the same with arrays as well

[source, ceylon]
----
Code not translatable : Unsupported kind ENUM_CONSTANT
----

You can also decode POJOs

[source, ceylon]
----
parser.handler((JsonEvent event) {
  // Handle each object
  // Get the field in which this object was parsed
  value id = event.fieldName();
  value user = event.mapTo(User.class);
  print("User with id ``id`` : ``user.firstName`` ``user.lastName``");
});

----

Whenever the parser fails to process a buffer, an exception will be thrown unless you set an exception handler:

[source, ceylon]
----
import io.vertx.ceylon.core.parsetools { jsonParser } 

...


value parser = jsonParser.newParser();

parser.exceptionHandler((Throwable err) {
  // Catch any parsing or decoding error
});

----

The parser also parses json streams:

- concatenated json streams: `{"temperature":30}{"temperature":50}`
- line delimited json streams: `{"an":"object"}\r\n3\r\n"a string"\r\nnull`

For more details, check out the `link:../../ceylondoc/vertx-core//parsetools/JsonParser.type.html[JsonParser]` class.