com.segment.analytics.Cartographer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of analytics Show documentation
Show all versions of analytics Show documentation
The hassle-free way to add analytics to your Android app.
/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Segment, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.segment.analytics;
import android.util.JsonReader;
import android.util.JsonToken;
import android.util.JsonWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Cartographer creates {@link Map} from JSON encoded streams and encodes Maps to their JSON
* representation.
*/
class Cartographer {
static final Cartographer INSTANCE = new Cartographer();
private Cartographer() {
}
/**
* Deserializes the specified json into a {@link Map}. If you have the Json in a {@link Reader}
* form instead of a {@link String}, use {@link #fromJson(Reader)} instead.
*/
Map fromJson(String json) throws IOException {
return fromJson(new StringReader(json));
}
/**
* Deserializes the json read from the specified {@link Reader} into a {@link Map}. If you have
* the Json in a String form instead of a {@link Reader}, use {@link #fromJson(String)} instead.
*/
Map fromJson(Reader reader) throws IOException {
JsonReader jsonReader = new JsonReader(reader);
try {
return readerToMap(jsonReader);
} finally {
reader.close();
}
}
/**
* Serializes the map into it's json representation and returns it as a String. If you want to
* write the json to {@link Writer} instead of retrieving it as a String, use {@link #toJson(Map,
* Writer)} instead.
*/
String toJson(Map, ?> map) throws IOException {
StringWriter stringWriter = new StringWriter();
toJson(map, stringWriter);
return stringWriter.toString();
}
/**
* Serializes the map into it's json representation into the provided {@link Writer}. If you want
* to retrieve the json as a string, use {@link #toJson(Map)} instead.
*/
void toJson(Map, ?> map, Writer writer) throws IOException {
if (map == null) {
throw new IllegalArgumentException("map == null");
}
JsonWriter jsonWriter = new JsonWriter(writer);
try {
mapToWriter(map, jsonWriter);
} finally {
jsonWriter.close();
}
}
// Decoding
/** Reads the {@link JsonReader} into a {@link Map}. */
private Map readerToMap(JsonReader reader) throws IOException {
Map map = new LinkedHashMap<>();
reader.beginObject();
while (reader.hasNext()) {
map.put(reader.nextName(), readValue(reader));
}
reader.endObject();
return map;
}
/** Reads the {@link JsonReader} into a {@link List}. */
private List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy