public class HealthDataUtil extends Object
HealthDataUtil makes compressed JSON conversion easy. Suppose LiveData class that defines as the HealthConstants.Exercise.LIVE_DATA's JSON format.
HealthDataUtil
LiveData
HealthConstants.Exercise.LIVE_DATA
public class LiveData { long start_time = 0L; float heart_rate = 0.f; float cadence = 0.f; float count = 0.f; float power = 0.f; float speed = 0.f; }
Make sure to add GSON as a Maven dependency to your project:
dependencies { ... implementation "com.google.code.gson:gson:2.7" }
LIVE_DATA
public byte[] createLiveData(List<LiveData> liveDataList) { // convert data class to JSON string byte[] zip = HealthDataUtil.getJsonBlob(liveDataList); return zip; }
public List<LiveData> getLiveData(byte[] zip) { // decompress ZIP List<LiveData> liveDataList = HealthDataUtil.getStructuredDataList(zip, LiveData.class); return liveDataList; }
static <T> byte[]
getJsonBlob(List<T> structuredDataList)
getJsonBlob(T structuredData)
static <T> T
getStructuredData(byte[] jsonBlob, Class<T> clazz)
static <T> List<T>
getStructuredDataList(byte[] jsonBlob, Class<T> clazz)
public static <T> byte[] getJsonBlob(T structuredData)
structuredData
IllegalStateException
public static <T> byte[] getJsonBlob(List<T> structuredDataList)
structuredDataList
public static <T> T getStructuredData(byte[] jsonBlob, Class<T> clazz)
jsonBlob
clazz
T
IllegalArgumentException
public static <T> List<T> getStructuredDataList(byte[] jsonBlob, Class<T> clazz)