JsonEncoder

org.sigilaris.core.codec.json.JsonEncoder
See theJsonEncoder companion object
trait JsonEncoder[A]

Type class for encoding Scala values into the core JsonValue AST.

Encoders consult a JsonConfig to honor naming policies, null/absent handling, discriminator strategy, and number formatting for big numbers.

Use contramap to adapt existing encoders and derived to auto-derive for products and sums when appropriate givens are in scope.

Attributes

See also

JsonDecoder for the inverse operation

JsonCodec for bidirectional encoding and decoding

JsonConfig for configuration options

Note

Encoding is deterministic: the same value always produces the same JSON structure. However, object field order may vary. Use sorted encoding if field order matters.

Example
 case class User(name: String, age: Int) derives JsonEncoder
 val user = User("Alice", 30)
 val json = JsonEncoder[User].encode(user)
Companion
object
Source
JsonEncoder.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Abstract methods

def encode(value: A): JsonValue

Encodes a Scala value to a JSON value.

Encodes a Scala value to a JSON value.

Value parameters

value

the value to encode

Attributes

Returns

the JSON representation

Source
JsonEncoder.scala

Concrete methods

def contramap[B](f: B => A): JsonEncoder[B]

Creates a new encoder by applying a function before encoding.

Creates a new encoder by applying a function before encoding.

Value parameters

f

the preprocessing function

Attributes

Returns

a new encoder for type B

Example
 val intEncoder: JsonEncoder[Int]       = JsonEncoder[Int]
 val lengthEncoder: JsonEncoder[String] = intEncoder.contramap(_.length)
Source
JsonEncoder.scala