Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.launchdarkly.sdk.internal;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.launchdarkly.sdk.internal.fdv2.payloads.IntentCode;

/**
* General-purpose Gson helpers.
*/
public abstract class GsonHelpers {
private static final Gson GSON_INSTANCE = new Gson();
private static final Gson GSON_INSTANCE = new GsonBuilder()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For other types I think this was handled by a type adapter factory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a dummy, I clicked merge before I saw this. Let me take a look.

.registerTypeAdapter(IntentCode.class, new IntentCode.IntentCodeTypeAdapter())
.create();

/**
* A singleton instance of Gson with the default configuration.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.launchdarkly.sdk.internal.fdv2.payloads;

import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.launchdarkly.sdk.json.SerializationException;

import java.io.IOException;
import java.util.Objects;

/**
* Represents the delete-object event, which contains a payload object that should be deleted.
*/
public final class DeleteObject {
private final int version;
private final String kind;
private final String key;

/**
* Constructs a new DeleteObject.
*
* @param version the minimum payload version this change applies to
* @param kind the kind of object being deleted ("flag" or "segment")
* @param key the identifier of the object being deleted
*/
public DeleteObject(int version, String kind, String key) {
this.version = version;
this.kind = Objects.requireNonNull(kind, "kind");
this.key = Objects.requireNonNull(key, "key");
}

/**
* Returns the minimum payload version this change applies to.
*
* @return the version
*/
public int getVersion() {
return version;
}

/**
* Returns the kind of the object being deleted ("flag" or "segment").
*
* @return the kind
*/
public String getKind() {
return kind;
}

/**
* Returns the identifier of the object being deleted.
*
* @return the key
*/
public String getKey() {
return key;
}

/**
* Parses a DeleteObject from a JsonReader.
*
* @param reader the JSON reader
* @return the parsed DeleteObject
* @throws SerializationException if the JSON is invalid
*/
public static DeleteObject parse(JsonReader reader) throws SerializationException {
Integer version = null;
String kind = null;
String key = null;

try {
if (reader.peek() != JsonToken.BEGIN_OBJECT) {
throw new SerializationException("expected object");
}
reader.beginObject();

while (reader.peek() != JsonToken.END_OBJECT) {
String name = reader.nextName();
switch (name) {
case "version":
version = reader.nextInt();
break;
case "kind":
kind = reader.nextString();
break;
case "key":
key = reader.nextString();
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();

if (version == null) {
throw new SerializationException("delete object missing required property 'version'");
}
if (kind == null) {
throw new SerializationException("delete object missing required property 'kind'");
}
if (key == null) {
throw new SerializationException("delete object missing required property 'key'");
}

return new DeleteObject(version, kind, key);
} catch (IOException | RuntimeException e) {
throw new SerializationException(e);
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.launchdarkly.sdk.internal.fdv2.payloads;

import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.launchdarkly.sdk.json.SerializationException;

import java.io.IOException;
import java.util.Objects;

/**
* Represents the error event, which indicates an error encountered server-side affecting
* the payload transfer. SDKs must discard partially transferred data. The SDK remains
* connected and expects the server to recover.
*/
public final class Error {
private final String id;
private final String reason;

/**
* Constructs a new Error.
*
* @param id the unique string identifier of the entity the error relates to
* @param reason human-readable reason the error occurred
*/
public Error(String id, String reason) {
this.id = id;
this.reason = Objects.requireNonNull(reason, "reason");
}

/**
* Returns the unique string identifier of the entity the error relates to.
*
* @return the identifier, or null if not present
*/
public String getId() {
return id;
}

/**
* Returns the human-readable reason the error occurred.
*
* @return the reason
*/
public String getReason() {
return reason;
}

/**
* Parses an Error from a JsonReader.
*
* @param reader the JSON reader
* @return the parsed Error
* @throws SerializationException if the JSON is invalid
*/
public static Error parse(JsonReader reader) throws SerializationException {
String id = null;
String reason = null;

try {
if (reader.peek() != JsonToken.BEGIN_OBJECT) {
throw new SerializationException("expected object");
}
reader.beginObject();

while (reader.peek() != JsonToken.END_OBJECT) {
String name = reader.nextName();
switch (name) {
case "id":
id = reader.nextString();
break;
case "reason":
reason = reader.nextString();
break;
default:
reader.skipValue();
break;
}
}
reader.endObject();

if (reason == null) {
throw new SerializationException("error missing required property 'reason'");
}

return new Error(id, reason);
} catch (IOException | RuntimeException e) {
throw new SerializationException(e);
}
}
}

Loading