Skip to content

Commit

Permalink
Merge pull request #34 from hossain-khan/feat/add-parser
Browse files Browse the repository at this point in the history
[ADDED] New parser class to abstract out moshi specifics
  • Loading branch information
hossain-khan authored Mar 15, 2024
2 parents 9ddd17a + 007d48b commit 49af453
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 28 deletions.
42 changes: 42 additions & 0 deletions parser/src/main/kotlin/Parser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dev.hossain.timeline

import ZonedDateTimeAdapter
import com.squareup.moshi.Moshi
import dev.hossain.timeline.model.Records
import dev.hossain.timeline.model.SemanticTimeline
import dev.hossain.timeline.model.Settings

/**
* Parser to parse Google Location Timeline JSON to Kotlin objects.
*/
class Parser constructor() {
// Moshi instance with custom adapter to parse the timeline data.
private val moshi: Moshi =
Moshi.Builder()
.add(ZonedDateTimeAdapter())
.build()

/**
* Parse JSON string to [Records] object.
*/
fun parseRecords(json: String): Records {
val adapter = moshi.adapter(Records::class.java)
return adapter.fromJson(json)!!
}

/**
* Parse JSON string to [Settings] object.
*/
fun parseSettings(json: String): Settings {
val adapter = moshi.adapter(Settings::class.java)
return adapter.fromJson(json)!!
}

/**
* Parse JSON string to [SemanticTimeline] object.
*/
fun parseSemanticTimeline(json: String): SemanticTimeline {
val adapter = moshi.adapter(SemanticTimeline::class.java)
return adapter.fromJson(json)!!
}
}
2 changes: 1 addition & 1 deletion parser/src/main/kotlin/moshi/ZonedDateTimeAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.time.format.DateTimeFormatter
/**
* Moshi adapter for [ZonedDateTime].
*/
class ZonedDateTimeAdapter {
internal class ZonedDateTimeAdapter {
@ToJson
fun toJson(zonedDateTime: ZonedDateTime): String {
return DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zonedDateTime)
Expand Down
10 changes: 3 additions & 7 deletions parser/src/test/kotlin/dev/hossain/timeline/model/RecordsTest.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package dev.hossain.timeline.model

import ZonedDateTimeAdapter
import com.google.common.truth.Truth.assertThat
import com.squareup.moshi.Moshi
import dev.hossain.timeline.Parser
import kotlin.test.Test

/**
* Test cases for [Settings].
*/
class RecordsTest {
private val moshi: Moshi =
Moshi.Builder()
.add(ZonedDateTimeAdapter())
.build()
private val parser = Parser()

@Test
fun `given records json should parse all records`() {
val json = javaClass.getResourceAsStream("/records.json")!!.bufferedReader().readText()
val records = moshi.adapter(Records::class.java).fromJson(json)!!
val records = parser.parseRecords(json)

assertThat(records.locations).hasSize(12)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package dev.hossain.timeline.model

import ZonedDateTimeAdapter
import com.google.common.truth.Truth.assertThat
import com.squareup.moshi.Moshi
import dev.hossain.timeline.Parser
import kotlin.test.Test

/**
* Test cases for [SemanticTimeline].
*/
class SemanticTimelineTest {
private val moshi: Moshi = Moshi.Builder().add(ZonedDateTimeAdapter()).build()
private val parser = Parser()

@Test
fun `given semantic timeline json should parse all timeline objects`() {
val timeline = timelineFromJson("/semantic-2021-august.json")
val timeline: SemanticTimeline = parser.parseSemanticTimeline(loadJson("/semantic-2021-august.json"))

assertThat(timeline.timelineObjects).hasSize(125)
}

@Test
fun `given activity segment from semantic timeline json - parsers activity segment data`() {
val timeline = timelineFromJson("/semantic-2021-august.json")
val timeline: SemanticTimeline = parser.parseSemanticTimeline(loadJson("/semantic-2021-august.json"))

val activitySegment = timeline.timelineObjects.first().activitySegment!!
assertThat(activitySegment.activityType).isEqualTo("IN_PASSENGER_VEHICLE")
Expand All @@ -32,7 +31,7 @@ class SemanticTimelineTest {

@Test
fun `given place visit from semantic timeline json - parsers place visit data`() {
val timeline = timelineFromJson("/semantic-2021-august.json")
val timeline: SemanticTimeline = parser.parseSemanticTimeline(loadJson("/semantic-2021-august.json"))

val placeVisit = timeline.timelineObjects[1].placeVisit!!
assertThat(placeVisit.location.latitudeE7).isEqualTo(438865167)
Expand All @@ -53,9 +52,7 @@ class SemanticTimelineTest {
/**
* Parses the given JSON file and returns [SemanticTimeline] object.
*/
private fun timelineFromJson(jsonFile: String): SemanticTimeline {
val json = javaClass.getResourceAsStream(jsonFile)!!.bufferedReader().readText()
val timeline = moshi.adapter(SemanticTimeline::class.java).fromJson(json)!!
return timeline
private fun loadJson(jsonFile: String): String {
return javaClass.getResourceAsStream(jsonFile)!!.bufferedReader().readText()
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package dev.hossain.timeline.model

import ZonedDateTimeAdapter
import com.google.common.truth.Truth.assertThat
import com.squareup.moshi.Moshi
import dev.hossain.timeline.Parser
import kotlin.test.Test

/**
* Test cases for [Settings].
*/
class SettingsTest {
private val moshi: Moshi = Moshi.Builder().add(ZonedDateTimeAdapter()).build()
private val parser = Parser()

@Test
fun `given settings json should parse all settings data`() {
val json = javaClass.getResourceAsStream("/settings.json")!!.bufferedReader().readText()
val settings = moshi.adapter(Settings::class.java).fromJson(json)!!
val settings: Settings = parser.parseSettings(json)

assertThat(settings.createdTime).isEqualTo("2013-08-10T18:07:41.251Z")
assertThat(settings.deviceSettings).hasSize(4)
Expand Down
15 changes: 9 additions & 6 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
plugins {
kotlin("jvm")
kotlin("jvm")
}

group = "dev.hossain.timeline"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
mavenCentral()
}

dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-test")
implementation(project(":parser"))
implementation("com.squareup.moshi:moshi-kotlin:1.15.1")
implementation("com.jakewharton.picnic:picnic:0.7.0")
testImplementation("org.jetbrains.kotlin:kotlin-test")
}

tasks.test {
useJUnitPlatform()
useJUnitPlatform()
}
kotlin {
jvmToolchain(21)
}
jvmToolchain(21)
}

0 comments on commit 49af453

Please sign in to comment.