Jan 2 2020

Human JSON

JSON is a versatile format to define data. I read and write JSON all the time but as a subset of JavaScript, it has a few limitations, most notably the lack of support for comments. Enter HJSON, or Human JSON. HJSON makes quotes around object names optional, it makes quotes around strings optional as well, it adds support for multi-line strings, and allows you to add comments in a number of ways. The following is valid HJSON.

  // use #, // or /**/ comments,
  // omit quotes for keys
  key: 1
  // omit quotes for strings
  contains: everything on this line
  // omit commas at the end of a line
  cool: {
    foo: 1
    bar: 2
  // allow trailing commas
  list: [
  // and use multiline strings
    My half empty glass,
    I will fill your empty half.
    Now you are half full.

There is an implementation of HJSON for most popular languages, like Java, Python, Go, Python, etc. I’ve used the hjson-java library to read HJSON file and converted it to JSON in a few lines.

Dec 27 2019

Create a HTTP Server in Java

Here is a quick tutorial on how to create a local embedded HTTP server in Java to serve JSON data. The whole code in less than 30 lines on Java.

package com.juixe.json.server;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.IOException;
import java.net.InetSocketAddress;

public class JSONServer implements HttpHandler {

    public static void main(String[] args) throws IOException  {
        HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
        server.createContext("/data", new JSONServer());

    public void handle(HttpExchange he) throws IOException {
        String response = "{\"message\": \"Hello, HTTP Client\"}";

        he.getResponseHeaders().set("Content-Type", "application/json;charset=UTF-8");
        he.sendResponseHeaders(200, response.length());

The HttpServer and HttpHandler classes are part of the JRE, so no additional libraries are required. Java also includes a HttpsServer which supports SSL. In the HttpHandler, you can return any data but for this example we are returning a simple JSON response.

Oct 8 2009

Jamming with Ruby YAML

When working with Ruby, the library/class I use and abuse most often is YAML. YAML stands for YAML Ain’t Markup Language and it is a versatile human friendly data serialization format. It is easier to use and understand than JSON.

A YAML file is much like a Java properties file in that is used to store name/value pairs. YAML is more powerful than simple Java properties file but that is a good way to think of it to begin with. Here is a example of a simple YAM file used to store user name and password.

user: juixe-username
pass: juixe-password

The above YAML snippet can go into a file, typically with a yml extension. To load the YAML file in ruby you can do it in with following Ruby code.

require 'yaml'

yml = YAML::load(File.open('userinfo.yml'))
puts yml['user'] # juixe-username

Just replace userinfo.yml with the name and path of your YAML file. The object that is loaded from the YAML file is a regular Ruby hash object so you can iterate through all the name/value pairs like the following.

require 'yaml'

yml = YAML.load_file 'userinfo.yml'
yml.each_pair { |key, value|
  puts "#{key} = #{value}"

What makes YAML files more powerful than a regular Java properties file is that you can complex object collections, structures, or hierarchies. For example, imagine that I want to log into a series of Twitter accounts and get their most recent at replies. I can keep a collection of twitter account usernames and passwords in a YAML file much like the following.

 user: juixe-user
 pass: juixe-pass
 user: techknow-user
 pass: techknow-pass

Here is the sample Ruby code that can be used to iterate through each user account from the YAML file.

require 'yaml'

yml = YAML.load_file 'userinfo.yml'
yml.each_key { |key|
  username = yml[key]['user']
  password = yml[key]['pass']

  puts "#{username} => #{password}"
  # login ...

You build more complex data structures than this using YAML, but this should be enough to get you going.