-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into update/scala-library-2.13.12
- Loading branch information
Showing
11 changed files
with
241 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version = 3.7.12 | ||
version = 3.7.17 | ||
|
||
style = default | ||
|
||
|
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
66 changes: 66 additions & 0 deletions
66
json/src/main/scala/org/http4s/fs2data/json/JsonInstances.scala
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,66 @@ | ||
/* | ||
* Copyright 2023 http4s.org | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.http4s | ||
package fs2data.json | ||
|
||
import cats.data.NonEmptyList | ||
import cats.effect.Concurrent | ||
import cats.syntax.applicative._ | ||
import cats.syntax.monadError._ | ||
import fs2.data.json._ | ||
import fs2.Stream | ||
import cats.syntax.show._ | ||
import org.http4s.Charset.`UTF-8` | ||
import org.http4s.headers.{`Content-Type`, `Transfer-Encoding`} | ||
|
||
trait JsonInstances { | ||
|
||
implicit def jsonTokensDecoder[F[_]: Concurrent]: EntityDecoder[F, Stream[F, Token]] = | ||
EntityDecoder.decodeBy(MediaType.application.json) { msg => | ||
DecodeResult.successT( | ||
msg.bodyText | ||
.through(tokens) | ||
.adaptError { case ex: JsonException => | ||
MalformedMessageBodyFailure( | ||
s"Invalid Json (${ex.context.fold("No context")(jc => jc.show)}): ${ex.msg}", | ||
Some(ex), | ||
) | ||
} | ||
) | ||
} | ||
|
||
def jsonTokensEncoder[F[_]](prettyPrint: Boolean)(implicit | ||
charset: Charset = `UTF-8` | ||
): EntityEncoder[F, Stream[F, Token]] = EntityEncoder.encodeBy( | ||
Headers( | ||
`Content-Type`(MediaType.application.json).withCharset(charset), | ||
`Transfer-Encoding`(TransferCoding.chunked.pure[NonEmptyList]), | ||
) | ||
) { tokens => | ||
Entity( | ||
tokens | ||
.through( | ||
if (prettyPrint) render.pretty() else render.compact | ||
) | ||
.through(fs2.text.encode[F](charset.nioCharset)) | ||
) | ||
} | ||
|
||
implicit def jsonTokensEncoder[F[_]]: EntityEncoder[F, Stream[F, Token]] = | ||
jsonTokensEncoder(prettyPrint = false) | ||
|
||
} |
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,19 @@ | ||
/* | ||
* Copyright 2023 http4s.org | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.http4s.fs2data | ||
|
||
package object json extends JsonInstances |
55 changes: 55 additions & 0 deletions
55
json/src/test/scala/org/http4s/fs2data/json/JsonEventSuite.scala
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,55 @@ | ||
package org.http4s.fs2data.json | ||
|
||
import cats.effect.IO | ||
import cats.syntax.all._ | ||
import fs2.Stream | ||
import fs2.data.json._ | ||
import fs2.data.json.literals.JsonInterpolator | ||
import munit.CatsEffectSuite | ||
import munit.ScalaCheckEffectSuite | ||
import org.http4s.EntityDecoder | ||
import org.http4s.Request | ||
|
||
class JsonEventSuite extends CatsEffectSuite with ScalaCheckEffectSuite { | ||
test("round-trip Json") { | ||
val in = json"""{"a": 1, "b": [true, false, null], "c": {"d": "e"}, "d": 1.2e3, "b": null}""" | ||
Stream | ||
.force(Request[IO]().withEntity(in.lift[IO]).as[Stream[IO, Token]]) | ||
.compile | ||
.toList | ||
.map(_.asRight[Throwable]) | ||
.assertEquals(in.toList) | ||
} | ||
|
||
test("round-trip Json string") { | ||
val in = """{"a":1,"b":[true,false,null],"c":{"d":"e"},"d":1.2e3,"b":null}""" | ||
Request[IO]() | ||
.withEntity(in) | ||
.as[Stream[IO, Token]] | ||
.map(Request[IO]().withEntity(_)) | ||
.flatMap(EntityDecoder.text[IO].decode(_, false).value) | ||
.assertEquals(Right(in)) | ||
} | ||
|
||
test("round-trip Json pretty printing") { | ||
val in = """{ | ||
| "a": 1, | ||
| "b": [ | ||
| true, | ||
| false, | ||
| null | ||
| ], | ||
| "c": { | ||
| "d": "e" | ||
| }, | ||
| "d": 1.2e3, | ||
| "b": null | ||
|}""".stripMargin | ||
Request[IO]() | ||
.withEntity(in) | ||
.as[Stream[IO, Token]] | ||
.map(Request[IO]().withEntity(_)(jsonTokensEncoder[IO](prettyPrint = true))) | ||
.flatMap(EntityDecoder.text[IO].decode(_, false).value) | ||
.assertEquals(Right(in)) | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.9.4 | ||
sbt.version=1.9.8 |
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