package json
This package provides extensions to the core "spac" library which allow for the handling of JSON data.
Rather than creating explicit classes that extend Parser
, Transformer
, and Splitter
,
this package provides type aliases and implicit extensions.
For example, JsonParser[A]
is just a type alias for Parser[JsonEvent, A]
,
and JsonParser
is just a call to Parser[JsonEvent]
.
Implicit JsonParsers are available for each of the JSON primitive types:
string
number
(expressed asInt
,Long
,Float
, orDouble
)boolean
null
(expressed asNone.type
)
Helpers are available for parsing JSON arrays and objects:
JsonParser.listOf[A]
to parse anarray
where each value is an
AJsonParser.objectOf[A]
to parse anobject
where the value for each field anA
JsonParser.objectOfNullable[A]
to parse anobject
where the value for each field is eithernull
or anA
, filtering out thenull
sJsonParser.fieldOf[A](fieldName)
to parse a specific field from an object
A DSL for creating json-specific ContextMatchers is provided to make it more convenient to call Splitter.json
.
For example:
Splitter.json("foo" \ "bar").as[String].parseFirst
Can be used to capture rootJson.foo.bar
as a String in
{ "foo": { "bar": "hello" } }
To "split" values inside arrays, index-related context matchers are available, e.g.
Splitter.json("foo" \ anyIndex).as[Int].parseToList
Can be used to capture each of the numbers in the "foo" array in
{ "foo": [1, 2, 3] }
A note about JsonEvents in spac: JSON doesn't have any explicit markers for when a field ends, or when an array index starts or ends; those context changes are essentially inferred by the presence of some other event. For example, instead of a "field end" event, typically there will be either a new "field start" or a token representing the end of the current object. With spac, splitters and context matchers generally operate under the assumption that a "stack push" event (like a field start) will eventually be followed by a corresponding "stack pop" event (i.e. field end).
To allow for this, these "inferred" events (FieldEnd, IndexStart, IndexEnd) are explicitly represented as JsonEvents in the stream being parsed. Keep this in mind when creating JSON ContextMatchers:
field
-related matchers will match a stack likecase ObjectStart :: FieldStart(_) :: _
index
-related matchers will match a stack likecase ArrayStart :: IndexStart(_) :: _
- Source
- package.scala
- Grouped
- Alphabetic
- By Inheritance
- json
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
- type JsonContextMatcher[+C] = ContextMatcher[JsonStackElem, C]
- sealed trait JsonEvent extends HasLocation
ADT for tokens in a JSON stream.
- type JsonParser[+Out] = Parser[JsonEvent, Out]
Type alias for a
Parser
whose input type isJsonEvent
- implicit final class JsonParserApplyOps extends AnyVal
Provides JSON-specific Parser constructor methods to the
JsonParser
object, for exampleJsonParser.fieldOf
.Provides JSON-specific Parser constructor methods to the
JsonParser
object, for exampleJsonParser.fieldOf
.Technically
JsonParser
is not a companion object, it is a partially-applied version of theParser
companion object which binds the input type toJsonEvent
, so "companion methods" must instead be added as extension methods. - type JsonSplitter[+C] = Splitter[JsonEvent, C]
Type alias for a
Splitter
whose input type isJsonEvent
- implicit final class JsonSplitterApplyOps extends AnyVal
Adds
Splitter.json
, for constructing json context matcher-based JsonSplitters - implicit final class JsonSplitterOps[C] extends AnyVal
Adds
splitter.asNullable[A]
, for handling possibly-null values in a JSON substream - sealed trait JsonStackElem extends JsonEvent
Subset of JsonEvents that constitute a "context stack push".
- sealed trait JsonStackPop extends JsonEvent
Subset of JsonEvents that constitute a "context stack pop".
- type JsonTransformer[+Out] = Transformer[JsonEvent, Out]
Type alias for a
Transformer
whose input type isJsonEvent
. - sealed trait JsonValueEvent extends JsonEvent
Subset of JsonEvents that represent a primitive values
Value Members
- val JsonParser: ParserApplyWithBoundInput[JsonEvent]
Like the
Parser
companion object, but only for creating Parsers whose input type isJsonEvent
.Like the
Parser
companion object, but only for creating Parsers whose input type isJsonEvent
.- See also
- val JsonSplitter: SplitterApplyWithBoundInput[JsonEvent]
Like the
Splitter
companion object, but only for creatingSplitters
whose input type isJsonEvent
. - val JsonTransformer: TransformerApplyWithBoundInput[JsonEvent]
Like the
Transformer
companion object, but only for creatingTransformers
whose input type isJsonEvent
. - def anyField: JsonContextMatcher[String]
Context matcher that matches events with any JSON object field, capturing the name of the field as context.
Context matcher that matches events with any JSON object field, capturing the name of the field as context. Specifically this matcher looks for an
ObjectStart
followed by aFieldStart(name)
, for anyname
. - def anyIndex: JsonContextMatcher[Int]
Context matcher that matches events within a JSON array, at any index.
Context matcher that matches events within a JSON array, at any index. Specifically this matcher looks for an
ArrayStart
followed by anIndexStart(_)
in the JSON context stack. If the match succeeds, thei
index will be captured as a context value. - def field[A](contextFromName: (String) => Option[A]): JsonContextMatcher[A]
Context matcher that matches events within certain JSON object fields, extracting some context value based on the name of the matched field.
Context matcher that matches events within certain JSON object fields, extracting some context value based on the name of the matched field. Specifically this matcher looks for an
ObjectStart
followed by aFieldStart(name)
, then plugs thename
intocontextFromName
. IfcontextFromName
returns aSome
, the match succeeds and the value inside the Some is returned.- contextFromName
a function that extracts a context value based on the name of the JSON object field
- implicit def field(name: String): JsonContextMatcher[Unit]
Context matcher that matches events within a given JSON object field.
Context matcher that matches events within a given JSON object field. Specifically this matcher looks for an
ObjectStart
followed by aFieldStart(name)
in the JSON context stack.When calling
Splitter.json
, the fact that this method isimplicit
allows you to use the underlying field name directly instead of directly calling this method, e.g.Splitter.json("address" \ "street")
- name
the name of the field
- def fieldWhere(f: (String) => Boolean): JsonContextMatcher[String]
Context matcher that matches events within JSON object fields whose names case the given
f
predicate to returntrue
.Context matcher that matches events within JSON object fields whose names case the given
f
predicate to returntrue
. Specifically this matcher looks for anObjectStart
followed by aFieldStart(name)
wheref(name)
istrue
. If the match succeeds, the name of the field is also extracted as a context value.- f
A predicate applied to field names to determine whether the current JSON context stack matches
- def index[A](contextFromIndex: (Int) => Option[A]): JsonContextMatcher[A]
Context matcher that matches events within a JSON array, at certain indexes.
Context matcher that matches events within a JSON array, at certain indexes. Specifically this matcher looks for an
ArrayStart
followed by anIndexStart(i)
in the JSON context stack, where thei
from theIndexStart
is passed tocontextFromIndex
. IfcontextFromIndex(i)
returns aSome
, the match succeeds and the value inside theSome
is returned as a context value. - def index(i: Int): JsonContextMatcher[Unit]
Context matcher that matches events within a JSON array at a specific index.
Context matcher that matches events within a JSON array at a specific index. Specifically this matcher looks for an
ArrayStart
followed by anIndexStart(i)
in the JSON context stack. - def indexWhere(f: (Int) => Boolean): JsonContextMatcher[Int]
Context matcher that matches events within a JSON array, at certain indexes.
Context matcher that matches events within a JSON array, at certain indexes. Specifically this matcher looks for an
ArrayStart
followed by anIndexStart(i)
in the JSON context stack, wheref(i)
istrue
. If the match succeeds, thei
index will be captured as a context value. - implicit val jsonParserForPrimitiveBoolean: JsonParser[Boolean]
Implicit version of
JsonParser.forBoolean
- implicit val jsonParserForPrimitiveDouble: JsonParser[Double]
Implicit version of
JsonParser.forDouble
- implicit val jsonParserForPrimitiveFloat: JsonParser[Float]
Implicit version of
JsonParser.forFloat
- implicit val jsonParserForPrimitiveInt: JsonParser[Int]
Implicit version of
JsonParser.forInt
- implicit val jsonParserForPrimitiveLong: JsonParser[Long]
Implicit version of
JsonParser.forLong
- implicit val jsonParserForPrimitiveNull: JsonParser[None.type]
Implicit version of
JsonParser.forNull
- implicit val jsonParserForPrimitiveString: JsonParser[String]
Implicit version of
JsonParser.forString
- object Fs2DataSource
Provides helpers for creating FS2 streams of
io.dylemma.spac.json.JsonEvent
, using fs2-data-json as the underlying event provider.Provides helpers for creating FS2 streams of
io.dylemma.spac.json.JsonEvent
, using fs2-data-json as the underlying event provider.This helper is set up to mirror the layout of the corresponding Fs2DataSource helper in the fs2-data-xml support module, but since there's less customization involved in setting up a stream of fs2-data-json Token objects, there's a lot less going on in this object. The main functionality provided here is the
convert
pipe, and some small helpers to automatically call it.For example:
val charStream: Stream[IO, Char] = ??? // this... val jsonStream1: Stream[IO, JsonEvent] = { import fs2.data.json._ charStream .through(tokens) .through(Fs2DataSource.convertEvents) } // could be replaced with val jsonStream2: Stream[IO, JsonEvent] = { Fs2DataSource[IO](charStream) }
Essentially this helper just provides a convenient
apply
method that acceptsString
,Stream[F, Char]
,Stream[F, String]
, orStream[F, fs2.data.json.Token]
to return aStream[F, JsonEvent]
which aJsonParser
could then interact with. - object JacksonSource
Provides helpers for creating
fs2.Stream
andIterator
instances ofJsonEvent
from various underlying event sources, using the Jackson library as the underlying event parser.Provides helpers for creating
fs2.Stream
andIterator
instances ofJsonEvent
from various underlying event sources, using the Jackson library as the underlying event parser.The helpers in this object operate in terms of the
IntoJacksonJsonParser
typeclass, which defines logic for using asource
value to construct a Jackson JsonParser in a Resource. From there, the helpers take care of converting the jackson parser/event model to the spac JsonEvent model.For example:
val file = new File("./stuff.json") val jsonStream: Stream[IO, JsonEvent] = JacksonSource[IO](file)
- object JsonEvent