Skip to content

Instantly share code, notes, and snippets.

Avatar

Billy Sjöberg billybong

View GitHub Profile
@billybong
billybong / ReproTest.java
Created Nov 6, 2019
json-smart serialization fails without setter - treats bean field without setter as public
View ReproTest.java
import net.minidev.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class ReproTest {
public static void main(String[] args) {
MyData data = new MyData("a");
Map<String, Object> m = new HashMap<>();
m.put("data", data);
@billybong
billybong / JacksonBodyHandlers.java
Last active Nov 11, 2020
JacksonBodyHandler for parsing JSON responses directly from Java 11+ http clients
View JacksonBodyHandlers.java
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.http.HttpResponse;
import java.util.function.Function;
public class JacksonBodyHandlers {
private final ObjectMapper objectMapper;
View DiTest.java
var providesStubRemoteApi = new Factories(){
Optional<RemoteApi> remoteApi(){
RemoteApi remoteApi = MyStubRemoteApi();
return Optional.of(remoteApi);
}
}
var app = bootstrapper.bootstrap(config, providesStubRemoteApi);
View Bootstrapper.java
class Bootstrapper {
public Application bootStrap(Config cfg, Factories factories){
var someDependency = constructTheDependency();
var myService = factories.myService().orElseGet(() -> constructTheRealThing(someDependency));
}
}
@billybong
billybong / DependencyInjection.java
Last active Aug 23, 2018
DI without framework, Java
View DependencyInjection.java
interface Factories {
default Optional<MyService> myService(){return Optional.empty();}
default Optional<RemoteApi> remoteApi(){return Optional.empty();}
static Factories factories(){return new Factories(){};}
}
View request collapsor naive impl
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.actor
class RequestCollapser<in ARGUMENT, BATCH_RESP, out RETURN_TYPE>(
private val threshold: Int,
private val fn: suspend (Collection<ARGUMENT>) -> BATCH_RESP,
private val remapper: (ARGUMENT, BATCH_RESP) -> RETURN_TYPE
) {
View nashorn javascript http client
#!/usr/bin/env jjs
/*####################################################################################################################################
# As Nashorn does not have http capabilities through XMLHttpRequest (DOM API), we have to use regular Java classes instead.
# This sample shows how this can be acheived without depending on any third party libraries. Just a standard Java 8 JDK.
# Make sure to have JAVA_HOME/bin on your PATH for the shebang to work. Then just chmod +x away and run...
# Alternatively if you're on a non *nix OS, start with jjs -scritping httpsample.js
####################################################################################################################################*/
var url = "https://api.github.com/users/billybong/repos";
var response;