Parser

class Parser

Parser to parse Google Location Timeline JSON to Kotlin objects.

This class provides efficient parsing of Google Takeout Location History data by caching JsonAdapter instances to minimize reflection overhead on repeated parsing calls.

Performance Characteristics:

  • JsonAdapter instances are cached and reused for optimal performance

  • Thread-safe: Multiple threads can safely use the same Parser instance

  • Memory efficient: Single Moshi instance with pre-configured adapters

Usage Recommendations:

  • Reuse Parser instances when parsing multiple files of the same type

  • Use BufferedSource overloads for better I/O performance with large files

  • Consider using a single Parser instance across your application

Exception Handling:

  • Throws JsonDataException for malformed JSON or missing required fields

  • Returns non-null objects; null JSON input will throw NullPointerException

Sample usages for parsing different JSON types:

val parser = Parser()

// Efficient for parsing multiple files
val bufferedSourceRecords: BufferedSource = recordsFile.source().buffer()
val records = parser.parseRecords(bufferedSourceRecords)

// Reuse the same parser instance
val bufferedSourceSemantic: BufferedSource = semanticMonthFile.source().buffer()
val semanticTimeline = parser.parseSemanticTimeline(bufferedSourceSemantic)

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard

Parse JSON string to Records object.

fun parseRecords(bufferedSource: BufferedSource): Records

Parse JSON buffered source to Records object.

Link copied to clipboard

Parse JSON string to SemanticTimeline object.

fun parseSemanticTimeline(bufferedSource: BufferedSource): SemanticTimeline

Parse JSON buffered source to SemanticTimeline object.

Link copied to clipboard

Parse JSON string to Settings object.

fun parseSettings(bufferedSource: BufferedSource): Settings

Parse JSON buffered source to Settings object.

Link copied to clipboard

Parse JSON string to TimelineEdits object.

fun parseTimelineEdits(bufferedSource: BufferedSource): TimelineEdits

Parse JSON buffered source to TimelineEdits object.