-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom autocompleteResultDeserializer
- Loading branch information
1 parent
75b6ee3
commit 685b395
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/com/sourcegraph/cody/agent/protocol/AutocompleteResultUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.sourcegraph.cody.agent.protocol | ||
|
||
import com.google.gson.GsonBuilder | ||
import com.google.gson.JsonDeserializationContext | ||
import com.google.gson.JsonDeserializer | ||
import com.google.gson.JsonElement | ||
import com.google.gson.JsonObject | ||
import com.sourcegraph.cody.agent.protocol_generated.AutocompleteResult | ||
import java.lang.reflect.Type | ||
|
||
private val gson = GsonBuilder().create() | ||
|
||
val autocompleteResultDeserializer = | ||
JsonDeserializer { jsonElement: JsonElement?, _: Type, _: JsonDeserializationContext -> | ||
val j = jsonElement?.asJsonObject | ||
if (j == null || j.isJsonNull) { | ||
null | ||
} else if (j.isJsonPrimitive) { | ||
j.asString | ||
} else { | ||
val obj = JsonObject() | ||
obj.add("items", j["items"]) | ||
gson.fromJson(obj, AutocompleteResult::class.java) | ||
} | ||
} |