-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix devfile component preferences deserialization through GSON
Signed-off-by: Max Shaposhnik <mshaposh@redhat.com>
- Loading branch information
1 parent
154d668
commit ec76c53
Showing
4 changed files
with
136 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
64 changes: 64 additions & 0 deletions
64
...api-dto/src/main/java/org/eclipse/che/dto/server/SerializableInterfaceAdapterFactory.java
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,64 @@ | ||
/* | ||
* Copyright (c) 2012-2018 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.dto.server; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.TypeAdapterFactory; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
import java.io.IOException; | ||
import java.io.Serializable; | ||
|
||
/** | ||
* This adapter is required for fields of {@link java.io.Serializable} type to be treated as {@link | ||
* Object} | ||
*/ | ||
public class SerializableInterfaceAdapterFactory implements TypeAdapterFactory { | ||
|
||
@Override | ||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
if (Serializable.class.equals(type.getRawType())) { | ||
return (TypeAdapter<T>) new SerializableAdapter(gson.getAdapter(Object.class)); | ||
} | ||
return null; | ||
} | ||
|
||
private static class SerializableAdapter extends TypeAdapter<Object> { | ||
|
||
TypeAdapter objectAdapter; | ||
|
||
public SerializableAdapter(TypeAdapter objectAdapter) { | ||
this.objectAdapter = objectAdapter; | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter out, Object value) throws IOException { | ||
objectAdapter.write(out, value); | ||
} | ||
|
||
@Override | ||
public Object read(JsonReader in) throws IOException { | ||
JsonToken token = in.peek(); | ||
if (token.equals(JsonToken.NUMBER)) { | ||
try { | ||
return in.nextLong(); | ||
} catch (NumberFormatException e) { | ||
return in.nextDouble(); | ||
} | ||
} | ||
return objectAdapter.read(in); | ||
} | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
core/che-core-api-dto/src/test/java/org/eclipse/che/dto/definitions/DtoWithSerializable.java
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,36 @@ | ||
/* | ||
* Copyright (c) 2012-2018 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.che.dto.definitions; | ||
|
||
import java.io.Serializable; | ||
import java.util.Map; | ||
import org.eclipse.che.dto.shared.DTO; | ||
|
||
/** DTO for testing serialization of fields with {@link java.io.Serializable} type */ | ||
@DTO | ||
public interface DtoWithSerializable { | ||
int getId(); | ||
|
||
DtoWithSerializable withId(int id); | ||
|
||
Serializable getObject(); | ||
|
||
void setObject(Serializable object); | ||
|
||
DtoWithSerializable withObject(Serializable object); | ||
|
||
Map<String, Serializable> getObjectMap(); | ||
|
||
void setObjectMap(Map<String, Serializable> map); | ||
|
||
DtoWithSerializable withObjectMap(Map<String, Serializable> map); | ||
} |