-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from tethys-json/dev-0.7.0
Release 0.7.0
- Loading branch information
Showing
18 changed files
with
536 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package tethys | ||
|
||
import tethys.readers.{FieldName, ReaderError} | ||
import tethys.readers.tokens.TokenIterator | ||
import tethys.writers.tokens.TokenWriter | ||
|
||
object JsonStreaming { | ||
|
||
def streamValue(from: TokenIterator, to: TokenWriter)(implicit fieldName: FieldName): Unit = writeCurrentValue(from, to) | ||
|
||
private def writeCurrentValue(it: TokenIterator, writer: TokenWriter)(implicit fieldName: FieldName): Unit = { | ||
val token = it.currentToken() | ||
if (token.isArrayStart) writeArray(it, writer) | ||
else if (token.isObjectStart) writeObject(it, writer) | ||
else if (token.isStringValue) writer.writeString(it.string()) | ||
else if (token.isNumberValue) writer.writeRawNumber(it.number()) | ||
else if (token.isBooleanValue) writer.writeBoolean(it.boolean()) | ||
else if (token.isNullValue) writer.writeNull() | ||
else ReaderError.wrongJson(s"Expects value start but $token found") | ||
it.next() | ||
} | ||
|
||
private def writeArray(it: TokenIterator, writer: TokenWriter)(implicit fieldName: FieldName): Unit = { | ||
it.next() | ||
writer.writeArrayStart() | ||
var index: Int = 0 | ||
while (!it.currentToken().isArrayEnd) { | ||
writeCurrentValue(it, writer)(fieldName.appendArrayIndex(index)) | ||
index = index + 1 | ||
} | ||
writer.writeArrayEnd() | ||
} | ||
|
||
private def writeObject(it: TokenIterator, writer: TokenWriter)(implicit fieldName: FieldName): Unit = { | ||
it.next() | ||
writer.writeObjectStart() | ||
while (!it.currentToken().isObjectEnd) { | ||
val token = it.currentToken() | ||
if(token.isFieldName) { | ||
val name = it.fieldName() | ||
writer.writeFieldName(name) | ||
writeCurrentValue(it.next(), writer)(fieldName.appendFieldName(name)) | ||
} else { | ||
ReaderError.wrongJson(s"Expects field name but $token found") | ||
} | ||
} | ||
writer.writeObjectEnd() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package tethys.commons | ||
|
||
import java.io.StringWriter | ||
|
||
import tethys.readers.FieldName | ||
import tethys.readers.tokens.TokenIterator | ||
import tethys.writers.tokens.{TokenWriter, TokenWriterProducer} | ||
import tethys.{JsonReader, JsonStreaming, JsonWriter} | ||
|
||
final case class RawJson(json: String) | ||
|
||
object RawJson { | ||
implicit val rawJsonWriter: JsonWriter[RawJson] = new JsonWriter[RawJson] { | ||
override def write(value: RawJson, tokenWriter: TokenWriter): Unit = tokenWriter.writeRawJson(value.json) | ||
} | ||
|
||
implicit def rawJsonReader(implicit tokenWriterProducer: TokenWriterProducer): JsonReader[RawJson] = new JsonReader[RawJson] { | ||
override def read(it: TokenIterator)(implicit fieldName: FieldName): RawJson = { | ||
val stringWriter = new StringWriter() | ||
val tokenWriter: TokenWriter = tokenWriterProducer.forWriter(stringWriter) | ||
JsonStreaming.streamValue(it, tokenWriter) | ||
tokenWriter.flush() | ||
RawJson(stringWriter.toString) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.