AutoValue Map

Additional

Language
Java
Version
N/A
Created
Feb 26, 2017
Updated
Feb 16, 2018
Owner
Ray Britton (raybritton)
Contributor
Ray Britton (raybritton)
1
Activity
Badge
Generate
Download
Source code

Promotion

AutoValue: Map Extension

An extension for Google's AutoValue that creates methods for converting to/from AutoValue models and Map<String, Object>.

Usage

Include auto-value-map in your project and add a Map<String, Object> toMap() method to your AutoValue object.

@AutoValue
public abstract class Person {
  public abstract String name();
  public abstract int age();

  //toMap() will be replaced
  public abstract Map<String, Object> toMap();

  //fromMap() is static so you need to create a method to call it.
  public static Person create(Map<String, Object> map) {
    return AutoValue_Person.fromMap(map);
  }
}

Types

Primitives, boxed primitives, strings, primtive arrays and enums (via @MapEnum) are supported by default. To support any other types, such as Integer[] you need to use a MapAdapter.

Full list of supported types:

  • byte, Byte, byte[]
  • int, Integer, int[]
  • long, Long, long[]
  • short, Short, Short[]
  • boolean, Boolean, boolean[]
  • float, Float, float[]
  • double, Double, double[]
  • char, Char, char[]
  • String
  • Enum (via @MapEnum)

Annotations

MapEnum Automatically converts enums to/from strings using name() and valueOf()

MapHide Prevents an element from being included the map, by default when creating an object from a map null be set for nullable hidden elements and 0 or false for primitive or non-nullable boxed primitives.

MapAdapter Provide an adapter for a element, this can replace the built in conversion for supported types as well. The adapter must be specified and the mapType must be set if not String. The mapType is the type for the data in the map. The adapter must convert from and to the method type and the map type.

MapElementName By default the map key is the method name this can be changed using @MapElementName

Example:

@AutoValue
public abstract class Car {
  public enum Colour {
    BLUE, RED, GREEN
  }

  @MapEnum
  public abstract Colour colour();
  @MapElementAdapter(adapter = StringListAdapter.class, mapType = String.class)
  public abstract List<String> owners();
  @MapHide
  public abstract Integer nullByDefault();
  @MapHide(SixMaker.class)
  public abstract Integer sixByDefault();
  @MapHide(value = SixMaker.class, readFromMap = true)
  public abstract Integer sixByDefaultUnlessMapContainsValue();
  @MapElementName("ADifferentName")
  public abstract String notThisName();
  ...
}

public class SixMaker implements MapValueMaker<Integer> {
  @Override
  public Integer make(String methodName) {
    return 6;
  }
}

public static class StringListAdapter implements MapAdapter<List<String>, String> {
  Gson gson = new Gson();
  Type typeToken = new TypeToken<List<String>>(){}.getType();

  @Override
  public List<String> fromMap(String methodName, String str) {
    return gson.fromJson(str, typeToken);
  }

  @Override
  public String toMap(String methodName, List<String> list) {
    return gson.toJson(list, typeToken);
  }
}

Download

Add a Gradle dependency:

ext.autoMapVersion = '2.3.1'

annotationProcessor "com.raybritton.autovaluemap:auto-value-map:$autoMapVersion"
provided "com.raybritton.autovaluemap:auto-value-map:$autoMapVersion"

// Optional needed if you wanted to use @MapHide, @MapElementName or @MapAdapter
compile "com.raybritton.autovaluemap:annotations:$autoMapVersion"

// Optional adapters for List<String>, List<Integer>, List<Double> and List<Boolean> which uses gson to serialize them
compile "com.raybritton.autovaluemap.gson:gson-parsers:$autoMapVersion"

License

Many thanks to Gabriel Ittner (for AutoValueCursor and in turn Ryan Harter (for AutoValueGson)

Copyright 2017 Ray Britton

Licensed 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

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.