public enum RealmNamingPolicy extends Enum<RealmNamingPolicy>
Examples where this is useful:
RealmModule.classNamingPolicy()
all classes part of that module
will be affected. If a class is part of multiple modules, the same naming policy must be
applied to both modules, otherwise an error will be thrown.
RealmModule.fieldNamingPolicy()
all persistable fields in all classes
part of this module will be affected.
RealmClass.fieldNamingPolicy()
all fields in that class will be
affected. This will override any field naming policy specified on a module.
An example of this:
\@RealmClass(name = "__person", fieldNamingPolicy = RealmNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
public class Person implements RealmModel { // is converted to "__person" internally
public string firstName; // Is converted to "first_name" internally
}
Choosing an internal name that differs from the name used in the Java model classes has the following implications:
DynamicRealm
must use the internal name. Queries on normal Realm
instances must continue to use the name as it is defined in the Java class.
When automatically converting Java variable names, each variable name is normalized by splitting it into a list of words that are then joined using the rules of the target format. The following heuristics are used for determining what constitutes a "word".
_
or $
is encountered.
Examples are "_FirstName", "_First_Name" and "$First$Name" which all becomes "First" and "Name".
Character.isUpperCase(int)
and Character.isLowerCase(int)
.
Example is "FirstName" which becomes "First" and "Name".
Character.isUpperCase(int)
and Character.isLowerCase(int)
.
Example is "FIRSTName" which becomes "FIRST" and "Name.
Note that changing the internal name does NOT affect importing data from JSON. The JSON data must still follow the names as defined in the Realm Java class.
When it comes to parsing JSON using standard libraries like Moshi, GSON or Jackson it is important to keep in mind that these libraries define the transformation from JSON to Java while setting internal Realm names define the transformation from Java to the Realm file.
This means that if you want to import data into Realm from JSON using these libraries you still need to provide the annotations from both the JSON parser library and Realm.
Using Moshi, it would look something like this:
public class Person extends RealmObject {
\@Json(name = "first_name") // Name used in JSON input.
\@RealmField(name = "first_name") // Name used internally in the Realm file.
public string firstName; // name used in Java
}
RealmModule
,
RealmClass
,
RealmField
Enum Constant and Description |
---|
CAMEL_CASE
The name in the Java model class is converted to camelCase, i.e.
|
IDENTITY
The name in the Java model class is used as is internally.
|
LOWER_CASE_WITH_UNDERSCORES
The name in the Java model class is converted lowercase with each word separated by
_ . |
NO_POLICY
No policy is applied.
|
PASCAL_CASE
The name in the Java model class is converted to PascalCase, i.e.
|
Modifier and Type | Method and Description |
---|---|
static RealmNamingPolicy |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static RealmNamingPolicy[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final RealmNamingPolicy NO_POLICY
RealmClass.fieldNamingPolicy()
, the module policy will still apply to field
names.
If two modules disagree on the policy and one of them is NO_POLICY
, the other will
be chosen without an error being thrown.
This policy is the default.
public static final RealmNamingPolicy IDENTITY
public static final RealmNamingPolicy CAMEL_CASE
Examples: "firstName", "FirstName", "mFirstName", "FIRST_NAME", "First$Name" all becomes "firstName".
public static final RealmNamingPolicy PASCAL_CASE
Examples: "firstName", "FirstName", "mFirstName", "FIRST_NAME", "First$Name" all becomes "FirstName".
public static final RealmNamingPolicy LOWER_CASE_WITH_UNDERSCORES
_
.
This is the default naming scheme in C++.
Examples: "firstName", "FirstName", "mFirstName", "FIRST_NAME", "First$Name" all becomes "first_name".
public static RealmNamingPolicy[] values()
for (RealmNamingPolicy c : RealmNamingPolicy.values()) System.out.println(c);
public static RealmNamingPolicy valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null