title: Default Values sidebar_position: 3 id: scala_default_values license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You 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
Fory supports Scala class default values during deserialization when using compatible mode. This feature enables forward/backward compatibility when case classes or regular Scala classes have default parameters.
When a Scala class has default parameters, the Scala compiler generates methods in the companion object (for case classes) or in the class itself (for regular Scala classes) like apply$default$1, apply$default$2, etc. that return the default values. Fory can detect these methods and use them when deserializing objects where certain fields are missing from the serialized data.
Fory supports default values for:
Detection: Fory detects if a class is a Scala class by checking for the presence of default value methods (apply$default$N or $default$N).
Default Value Discovery:
apply$default$1, apply$default$2, etc.$default$1, $default$2, etc.Field Mapping: During deserialization, Fory identifies fields that exist in the target class but are missing from the serialized data.
Value Application: After reading all available fields from the serialized data, Fory applies default values to any missing fields.
This feature is automatically enabled when:
withCompatibleMode(CompatibleMode.COMPATIBLE))No additional configuration is required.
import org.apache.fory.Fory import org.apache.fory.config.CompatibleMode import org.apache.fory.serializer.scala.ScalaSerializers // Class WITHOUT default values (for serialization) case class PersonV1(name: String) // Class WITH default values (for deserialization) case class PersonV2(name: String, age: Int = 25, city: String = "Unknown") val fory = Fory.builder() .withCompatibleMode(CompatibleMode.COMPATIBLE) .withScalaOptimizationEnabled(true) .build() ScalaSerializers.registerSerializers(fory) // Serialize using class without default values val original = PersonV1("John") val serialized = fory.serialize(original) // Deserialize into class with default values // Missing fields will use defaults val deserialized = fory.deserialize(serialized).asInstanceOf[PersonV2] // deserialized.name == "John" // deserialized.age == 25 (default) // deserialized.city == "Unknown" (default)
// Class WITHOUT default values (for serialization) class EmployeeV1(val name: String) // Class WITH default values (for deserialization) class EmployeeV2( val name: String, val age: Int = 30, val department: String = "Engineering" ) val fory = Fory.builder() .withCompatibleMode(CompatibleMode.COMPATIBLE) .withScalaOptimizationEnabled(true) .build() ScalaSerializers.registerSerializers(fory) // Serialize using class without default values val original = new EmployeeV1("Jane") val serialized = fory.serialize(original) // Deserialize into class with default values val deserialized = fory.deserialize(serialized).asInstanceOf[EmployeeV2] // deserialized.name == "Jane" // deserialized.age == 30 (default) // deserialized.department == "Engineering" (default)
Default values can be complex expressions:
// Class WITHOUT default values (for serialization) case class ConfigV1(name: String) // Class WITH default values (for deserialization) case class ConfigV2( name: String, settings: Map[String, String] = Map("default" -> "value"), tags: List[String] = List("default"), enabled: Boolean = true ) val fory = Fory.builder() .withCompatibleMode(CompatibleMode.COMPATIBLE) .withScalaOptimizationEnabled(true) .build() ScalaSerializers.registerSerializers(fory) val original = ConfigV1("myConfig") val serialized = fory.serialize(original) val deserialized = fory.deserialize(serialized).asInstanceOf[ConfigV2] // deserialized.name == "myConfig" // deserialized.settings == Map("default" -> "value") // deserialized.tags == List("default") // deserialized.enabled == true
object Models { // Class WITHOUT default values (for serialization) case class PersonV1(name: String) // Classes WITH default values (for deserialization) case class Address(street: String, city: String = "DefaultCity") case class PersonV2(name: String, address: Address = Address("DefaultStreet")) } val fory = Fory.builder() .withCompatibleMode(CompatibleMode.COMPATIBLE) .withScalaOptimizationEnabled(true) .build() ScalaSerializers.registerSerializers(fory) val original = Models.PersonV1("Alice") val serialized = fory.serialize(original) val deserialized = fory.deserialize(serialized).asInstanceOf[Models.PersonV2] // deserialized.name == "Alice" // deserialized.address == Address("DefaultStreet", "DefaultCity")