com.github.nmorel.gwtjackson.client.ObjectMapper Java Examples

The following examples show how to use com.github.nmorel.gwtjackson.client.ObjectMapper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Pojofy.java    From vertxui with GNU General Public License v3.0 6 votes vote down vote up
public static <I, O> void ajax(String protocol, String url, I model, ObjectMapper<I> inMapper,
		ObjectMapper<O> outMapper, BiConsumer<Integer, O> handler) {
	XMLHttpRequest xhr = XMLHttpRequest.create();
	xhr.setOnReadyStateChange(a -> {
		if (handler == null || xhr.getReadyState() != 4) {
			return;
		}
		O result = null;
		if (xhr.getStatus() == 200) {
			result = out(xhr.getResponseText(), outMapper);
		}
		handler.accept(xhr.getStatus(), result);
	});
	xhr.open(protocol, url);
	xhr.send(in(model, inMapper));
}
 
Example #2
Source File: Pojofy.java    From vertxui with GNU General Public License v3.0 5 votes vote down vote up
public static <O> boolean socketReceive(String url, Event e, ObjectMapper<O> outMapper, Consumer<O> handler) {
	Object me = ((MessageEvent) e).getData();
	String meString = me.toString();
	if (meString.equals("[object ArrayBuffer]")) { // websockets
		meString = socketToString((ArrayBuffer) me);
	}
	if (!meString.startsWith("{\"url\":\"" + url + "\"")) {
		return false;
	}
	JsonObject json = Json.parse(meString);
	handler.accept(out(json.getString("body"), outMapper));
	return true;
}
 
Example #3
Source File: Pojofy.java    From vertxui with GNU General Public License v3.0 5 votes vote down vote up
protected static <I> String in(I model, ObjectMapper<I> inMapper) {
	if (model == null) {
		return null;
	} else if (model instanceof String || inMapper == null) {
		return (String) model;
	} else {
		return inMapper.write(model);
	}
}
 
Example #4
Source File: Pojofy.java    From vertxui with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected static <O> O out(String message, ObjectMapper<O> outMapper) {
	if (message == null) {
		return null;
	} else if (outMapper == null) { // outMapper null: string
		return (O) message;
	} else {
		return outMapper.read(message);
	}
}
 
Example #5
Source File: Pojofy.java    From vertxui with GNU General Public License v3.0 5 votes vote down vote up
public static <I> void socketSend(WebSocket socket, String url, I model, ObjectMapper<I> inMapper,
		JsonObject headers) {
	JsonObject object = Json.createObject();
	object.put("url", url);
	object.put("body", in(model, inMapper));
	object.put("headers", headers);
	socket.send(object.toJson());
}
 
Example #6
Source File: Pojofy.java    From vertxui with GNU General Public License v3.0 5 votes vote down vote up
public static <O> void eventbusReceive(EventBus eventBus, String address, JsonObject headers,
		ObjectMapper<O> outMapper, Consumer<O> handler) {
	eventBus.registerHandler(address, headers, (error, m) -> {
		if (error != null) {
			throw new IllegalArgumentException(error.asString());
		}
		O output = out(m.getString("body"), outMapper);
		handler.accept(output);
	});
}
 
Example #7
Source File: Pojofy.java    From vertxui with GNU General Public License v3.0 4 votes vote down vote up
public static <I> void eventbusPublish(EventBus eventBus, String address, I model, JsonObject headers,
		ObjectMapper<I> inMapper) {
	eventBus.publish(address, in(model, inMapper), headers);
}
 
Example #8
Source File: JsonGwtJacksonGenerator.java    From requestor with Apache License 2.0 4 votes vote down vote up
private SourceWriter getSourceWriter(TreeLogger logger, GeneratorContext ctx, JClassType intfType) {
    JPackage serviceIntfPkg = intfType.getPackage();
    String packageName = serviceIntfPkg == null ? "" : serviceIntfPkg.getName();
    PrintWriter printWriter = ctx.tryCreate(logger, packageName, getTypeSimpleName());
    if (printWriter == null) {
        return null;
    }

    ClassSourceFileComposerFactory composerFactory =
            new ClassSourceFileComposerFactory(packageName, getTypeSimpleName());

    String[] imports = new String[]{
            // java
            ArrayList.class.getCanonicalName(),
            Collection.class.getCanonicalName(),
            HashSet.class.getCanonicalName(),
            Iterator.class.getCanonicalName(),
            LinkedHashSet.class.getCanonicalName(),
            LinkedList.class.getCanonicalName(),
            List.class.getCanonicalName(),
            Set.class.getCanonicalName(),
            TreeSet.class.getCanonicalName(),
            // com.github.nmorel.gwtjackson
            ObjectMapper.class.getCanonicalName(),
            ObjectReader.class.getCanonicalName(),
            ObjectWriter.class.getCanonicalName(),
            // com.google.gwt
            GWT.class.getCanonicalName(),
            // io.reinert.requestor
            DeserializationContext.class.getCanonicalName(),
            Deserializer.class.getCanonicalName(),
            JsonObjectSerdes.class.getCanonicalName(),
            JsonRecordReader.class.getCanonicalName(),
            JsonRecordWriter.class.getCanonicalName(),
            UnableToDeserializeException.class.getName(),
            UnableToSerializeException.class.getName(),
            Serdes.class.getCanonicalName(),
            Serializer.class.getCanonicalName(),
            SerializationContext.class.getCanonicalName()
    };

    for (String imp : imports) {
        composerFactory.addImport(imp);
    }

    composerFactory.addImplementedInterface(intfType.getErasedType().getQualifiedSourceName());

    return composerFactory.createSourceWriter(ctx, printWriter);
}
 
Example #9
Source File: Mechanism.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
@Override
public ObjectMapper<DataContainer> getMapper() {
    return singleton;
}
 
Example #10
Source File: Mechanism.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
@Override
public ObjectMapper<DataContainer> getMapper() {
    return newMapper();
}
 
Example #11
Source File: RestyGwt.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
@Override
protected ObjectMapper<DataContainer> newMapper() {
    decorator.mapper = GWT.create( DataContainerMapper.class );
    return decorator;
}
 
Example #12
Source File: GwtJackson.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
@Override
protected ObjectMapper<DataContainer> newMapper() {
    return GWT.create( DataContainerMapper.class );
}
 
Example #13
Source File: JsonRootNameGwtTest.java    From gwt-jackson with Apache License 2.0 4 votes vote down vote up
protected <T> ObjectMapperTester<T> createRootMapper( final ObjectMapper<T> mapper ) {
    return createMapper( mapper, JsonDeserializationContext.builder().unwrapRootValue( true ).build(), JsonSerializationContext
            .builder().wrapRootValue( true ).build() );
}
 
Example #14
Source File: Pojofy.java    From vertxui with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Warning: the thing you send can go to anyone connected to the eventbus,
 * including other browsers that are connected. So please handle with care!
 * 
 * @param <I>
 *            input type
 * @param <O>
 *            output type
 * @param eventBus
 *            the eventbus
 * @param address
 *            the address
 * @param model
 *            the model
 * @param headers
 *            the headers
 * @param inMapper
 *            the inputmapper
 * @param outMapper
 *            the outputmapper
 * @param handler
 *            the callback handler
 */
public static <I, O> void eventbusSend(EventBus eventBus, String address, I model, JsonObject headers,
		ObjectMapper<I> inMapper, ObjectMapper<O> outMapper, Consumer<O> handler) {
	eventBus.send(address, in(model, inMapper), headers, (error, m) -> {
		if (error != null) {
			throw new IllegalArgumentException(error.asString());
		}
		handler.accept(out(m.getString("body"), outMapper));
	});
}
 
Example #15
Source File: Mechanism.java    From gwt-jackson with Apache License 2.0 votes vote down vote up
ObjectMapper<DataContainer> getMapper(); 
Example #16
Source File: Mechanism.java    From gwt-jackson with Apache License 2.0 votes vote down vote up
protected abstract ObjectMapper<DataContainer> newMapper();