Repository: google/android-fhir
Issue: Not insert a local change row when the Resource diff is empty

**Describe the bug**
A clear and concise description of what the bug is.

**Component**
Core library

**To Reproduce**
Steps to reproduce the behavior:
1. Insert a new `Resource`
2. Do not modify the `Resource` instance
3. Update the `Resource` with `FhirEngine.update(resource)` 

**Expected behavior**
Since we didn't make any change and the JSON diff is an empty array : `[]`, no row should be inserted in the `LocalChangesEntity` table.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Smartphone (please complete the following information):**
Any device

**Additional context**
JSON diffs are being passed back as strings to the `LocalChangesDao` hence checks would be kludgy. Returning a `JSONArray` or other object will enable check for emptiness.


Changed files:
core/src/main/java/com/google/android/fhir/db/impl/dao/LocalChangeUtils.kt, core/src/main/java/com/google/android/fhir/db/impl/dao/LocalChangeDao.kt

diff --git a/core/src/main/java/com/google/android/fhir/db/impl/dao/LocalChangeDao.kt b/core/src/main/java/com/google/android/fhir/db/impl/dao/LocalChangeDao.kt
index 249a3193d..fac940d6b 100644
--- a/core/src/main/java/com/google/android/fhir/db/impl/dao/LocalChangeDao.kt
+++ b/core/src/main/java/com/google/android/fhir/db/impl/dao/LocalChangeDao.kt
@@ -16,6 +16,7 @@
 
 package com.google.android.fhir.db.impl.dao
 
+import android.util.Log
 import androidx.room.Dao
 import androidx.room.Insert
 import androidx.room.Query
@@ -76,7 +77,15 @@ internal abstract class LocalChangeDao {
         "Unexpected DELETE when updating $resourceType/$resourceId. UPDATE failed."
       )
     }
-
+    val jsonDiff = LocalChangeUtils.diff(iParser, oldResource, resource)
+    if (jsonDiff.length() == 0) {
+      Log.i(
+        "LocalChangeDao",
+        "New resource ${resource.resourceType}/${resource.id} is same as old resource. " +
+          "Not inserting UPDATE LocalChange."
+      )
+      return
+    }
     addLocalChange(
       LocalChangeEntity(
         id = 0,
@@ -84,7 +93,7 @@ internal abstract class LocalChangeDao {
         resourceId = resourceId,
         timestamp = timestamp,
         type = Type.UPDATE,
-        payload = LocalChangeUtils.diff(iParser, oldResource, resource)
+        payload = jsonDiff.toString()
       )
     )
   }
diff --git a/core/src/main/java/com/google/android/fhir/db/impl/dao/LocalChangeUtils.kt b/core/src/main/java/com/google/android/fhir/db/impl/dao/LocalChangeUtils.kt
index ac50632cc..0302d2b4f 100644
--- a/core/src/main/java/com/google/android/fhir/db/impl/dao/LocalChangeUtils.kt
+++ b/core/src/main/java/com/google/android/fhir/db/impl/dao/LocalChangeUtils.kt
@@ -16,7 +16,6 @@
 
 package com.google.android.fhir.db.impl.dao
 
-import android.util.Log
 import ca.uhn.fhir.parser.IParser
 import com.fasterxml.jackson.databind.JsonNode
 import com.fasterxml.jackson.databind.ObjectMapper
@@ -90,17 +89,14 @@ object LocalChangeUtils {
   }
 
   /** Calculates the JSON patch between two [Resource] s. */
-  internal fun diff(parser: IParser, source: Resource, target: Resource): String {
+  internal fun diff(parser: IParser, source: Resource, target: Resource): JSONArray {
     val objectMapper = ObjectMapper()
     val jsonDiff =
       JsonDiff.asJson(
         objectMapper.readValue(parser.encodeResourceToString(source), JsonNode::class.java),
         objectMapper.readValue(parser.encodeResourceToString(target), JsonNode::class.java)
       )
-    if (jsonDiff.size() == 0) {
-      Log.i("ResourceDao", "Target ${target.resourceType}/${target.id} is same as source.")
-    }
-    return jsonDiff.toString()
+    return JSONArray(jsonDiff.toString())
   }
 
   /**