PT 156967558 - Replace ObjectMapper with Gson in Bosh LS
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2018 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.commons.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
public class GsonUtil {
|
||||
|
||||
public static Stream<JsonElement> getFromElements(JsonElement target) {
|
||||
if (target.isJsonArray()) {
|
||||
return Streams.fromIterable(target.getAsJsonArray());
|
||||
} else if (target.isJsonObject()) {
|
||||
List<JsonElement> vals = new ArrayList<>();
|
||||
Set<Entry<String, JsonElement>> entrySet = target.getAsJsonObject().entrySet();
|
||||
if (entrySet != null) {
|
||||
for (Entry<String, JsonElement> entry : entrySet) {
|
||||
vals.add(entry.getValue());
|
||||
}
|
||||
}
|
||||
return vals.stream();
|
||||
} else {
|
||||
return Stream.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public static Stream<JsonElement> getFromIndex(JsonElement target, int index) {
|
||||
if (target.isJsonArray()) {
|
||||
return Streams.fromNullable(target.getAsJsonArray().get(index));
|
||||
} else {
|
||||
return Stream.empty();
|
||||
}
|
||||
}
|
||||
|
||||
public static Stream<JsonElement> getFromKey(JsonElement target, String key) {
|
||||
if (target.isJsonObject()) {
|
||||
return Streams.fromNullable(target.getAsJsonObject().get(key));
|
||||
} else {
|
||||
return Stream.empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2016-2017 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.commons.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
public class Streams {
|
||||
|
||||
/**
|
||||
* Returns the element in the stream, if the stream has exactly one element.
|
||||
* Otherwise returns null
|
||||
*/
|
||||
public static <T> T getSingle(Stream<T> stream) {
|
||||
ArrayList<T> els = stream
|
||||
.limit(2) //Don't need more than 2 to know there is more than 1
|
||||
.collect(Collectors.toCollection(() -> new ArrayList<>(2)));
|
||||
return els.size()==1 ? els.get(0) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Like java.util.Stream.of but returns Stream.empty if the element is null
|
||||
*/
|
||||
public static <T> Stream<T> fromNullable(T e) {
|
||||
return e==null ? Stream.empty() : Stream.of(e);
|
||||
}
|
||||
|
||||
public static <T> Stream<T> fromIterable(Iterable<T> target) {
|
||||
return StreamSupport.stream(target.spliterator(), false);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user