Serialization

Serializing the criteria instance can be useful for transferring the criteria data.

There are many use cases:

  • Appending criteria on a URL query string parameter.

  • Writing criteria to a file.

  • Saving criteria to the database.

Base64

The criteria can be serialized to Base64 format.

import io.github.magonxesp.criteria.infrastructure.serialization.encodeToBase64

val criteria = criteria {
   filter("author_name", "Svetlana Isakova", FilterOperator.EQUALS)
}

// serialize to Base64
val base64 = criteria.encodeToBase64()

The resulting Base64 representation will be as follows:

eyJmaWx0ZXJzIjpbeyJmaWVsZCI6ImF1dGhvcl9uYW1lIiwidmFsdWUiOiJTdmV0bGFuYSBJc2Frb3ZhIiwib3BlcmF0b3IiOiI9In1dLCJvcmRlckJ5IjpbXSwicGFnaW5hdGlvbiI6eyJwYWdlIjoxLCJzaXplIjpudWxsfX0=

Also, a Base64 encoded criteria can be deserialized.

JSON

The criteria can be serialized to JSON format, which serves as the initial serialization method before Base64 encoding.

The resulting JSON representation will be as follows:

And for deserialize the JSON-encoded criteria, we can use the deserializeCriteriaFromJson() extension function.

Last updated