Skip to content

Instantly share code, notes, and snippets.

View jonikarppinen's full-sized avatar

Joni Karppinen jonikarppinen

View GitHub Profile
@jonikarppinen
jonikarppinen / Question20442265.java
Last active December 30, 2015 17:39
Answer for this question about Gson: http://stackoverflow.com/questions/20442265/how-to-decode-json-with-unknown-field-using-gson. This demonstrates how to use custom deserialiser to parse a big list of properties of the same type but unknown/random/varying names. Besides JDK, depends on Guava library.
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.gson.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@jonikarppinen
jonikarppinen / markdown-comments.md
Last active April 21, 2024 23:44
How to comment out stuff in Markdown on GitHub?

Comments in GitHub flavour of Markdown

As answers to this Stack Overflow question reveal, using <!--- and ---> or <!-- and --> works (view source by clicking "Raw"):

@jonikarppinen
jonikarppinen / proguard-project.txt
Last active August 1, 2017 09:56
Example ProGuard config for a project using RxJava, Guava and Gson
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
@jonikarppinen
jonikarppinen / CustomErrorController.java
Last active June 16, 2021 02:19
Example of replacing Spring Boot "whitelabel" error page with custom error responses (with JSON response body)
package com.company.project.controllers;
import com.company.project.model.api.ErrorJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
@jonikarppinen
jonikarppinen / CustomObjectMapper.java
Last active June 28, 2018 21:28
Example of making Jackson behave more sensibly by default (also, more like Gson)
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
* ObjectMapper customised for my tastes and most typical needs
*
* @author Joni Karppinen
*/
@jonikarppinen
jonikarppinen / StringUtils.java
Last active August 29, 2015 14:20
StringUtils – common utility methods for Java
/**
* @author Joni Karppinen
* Licence: WTFPL, http://www.wtfpl.net/txt/copying/
*/
public class StringUtils {
public static String encodeUrlParameters(Map<String, String> parameters) {
if (parameters == null || parameters.isEmpty()) {
return "";
}
@jonikarppinen
jonikarppinen / PdfOrErrorController.java
Last active November 17, 2021 11:49
Example of using ExceptionHandler in Spring Boot: a controller method that returns either binary data or error JSON
package com.company.project.controllers;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Random;
@jonikarppinen
jonikarppinen / Messages.java
Last active February 23, 2021 23:36
Example of using message resources in Spring Boot service layer code, in as simple way as possible (hopefully!). NOTE: this approach supports only a single locale, not dynamically changing it.
package com.company.project.components;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Locale;
@jonikarppinen
jonikarppinen / Specs2-output.txt
Last active December 8, 2015 20:34
Specs2 output when run in IntelliJ IDEA: "Test framework quit unexpectedly" (see http://stackoverflow.com/questions/34159857/specs2-how-to-test-a-class-with-more-than-one-injected-dependency)
Testing started at 22:28 ...
services.ReportServiceSpec$
java.lang.ClassNotFoundException: services.ReportServiceSpec$
STACKTRACE
java.net.URLClassLoader.findClass(URLClassLoader.java:381)
java.lang.ClassLoader.loadClass(ClassLoader.java:424)
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
@jonikarppinen
jonikarppinen / 1_RxJava-connectivity-status-example.md
Last active November 23, 2022 18:16
Example of listening to connectivity status in Android and reacting to going offline. (Subscription, Observable, Observer, PublishSubject are from RxJava.)

Example of using RxJava to listen to connectivity status in Android

You could use the same approach to listen to any status, but this example includes network connectivity specifics too (ConnectionChangeReceiver and AndroidUtils.isConnected).