Skip to main content

JSON Integration

Forms4s is designed to support any json library but currently only circe integration is available.

Usage

"org.business4s" %% "forms4s-circe" % "0.1.0"
val formState: FormElementState = ???

// Extract form data as JSON
import forms4s.circe.*
val formData: Json = formState.extractJson

// Load JSON into a form
val rebuiltState: FormElementState = formState.load(formData)

Limitations

  • Currently hydration doesn't work for non-delimited alternatives
  • Hydration errors (e.g. unmatched fields) are silently ignored

Example values

Table below show json extracted from various form elements

FormElement Type Empty Value Example Value
Text
""
"foo"
Number (Decimal)
null
1.1
Select
"Option 1"
"Option 2"
Checkbox
false
true
Group
  • field1
{
  "field1" : ""
}
{
  "field1" : "foo"
}
Multivalue
[
]
[
  "foo"
]
Alternative
  • alt1
    • field1
  • alt2
    • field1
{
  "tpe" : "alt1",
  "field1" : ""
}
{
  "tpe" : "alt2",
  "field1" : "foo"
}