Skip to content

Java serialization (string and binary)

Brice Morin edited this page Apr 12, 2016 · 3 revisions

We should generate Message like this, to support string and binary (de)serialization. The default binary serialization should (hopefully) be compatible right away with C/Arduino serialization. Additional serialization plugins will add code in /*BINARY_LOAD*/ comment, and other similar comments.

It will produce:

{"timer_start":{"delay": 1000}}   //JSON
timer_start (delay: 1000)         //Another String serialization, just here for compatibility
[0, 3, -24]                       //binary

for a message timer_start with code 0 and taking a short (2 bytes) as parameter.

/**
 * File generated by the ThingML IDE
 * /!\Do not edit this file/!\
 * In case of a bug in the generated code,
 * please submit an issue on our GitHub
 **/

package org.thingml.generated.messages;

import org.thingml.java. * ;
import org.thingml.java.ext. * ;

import org.thingml.generated.api. * ;
import java.util. * ;
import java.nio.*;

public class Timer_startMessageType extends EventType {
	private final byte code;
	
	public Timer_startMessageType(byte code) { //TODO: modify compiler to call this constructor with a proper code
		super();
		this.code = code;
	}
	
	public Timer_startMessageType() { //just here for compatiblity (we should call constructor with code)
		name = "timer_start";
		this.code = 0x00;
	}

	public Event instantiate(final Port port, final short delay) {
		return new Timer_startMessage(this, port, delay);
	}
	
	@ Override
	public Event instantiate(Port port, Map < String, Object > params) {
		return instantiate(port, (Short)params.get("delay"));
	}
			
	public Event instantiate(Port port, byte[] payload, String serialization) {//TODO: to be generated
		if (serialization == null || serialization.equals("default")) {
			ByteBuffer buffer = ByteBuffer.wrap(payload);
			buffer.order(ByteOrder.BIG_ENDIAN);
			byte code = buffer.get();
			if (code == this.code) {
				short delay = buffer.getShort();
				return instantiate(port, delay);
			}
			return null;
		}
		/*BINARY_LOAD*/
		return null;
	}
	
	public Event instantiate(Port port, String payload, String serialization) {//TODO: to be generated
		if (serialization == null || serialization.equals("default")) {
			final String[] msg = payload.trim().replace(" ", "").replace("\t", "").replace("\n", "").replace("\"", "").replace(")", "").split("[(:,]+");	
			return parse(port, msg);
		} else if (serialization.equals("json-default")) {
			final String[] msg = payload.trim().replace(" ", "").replace("\t", "").replace("\n", "").replace("\"", "").replace("{", "").replace("}", "").split("[:,]+");			
			return parse(port, msg);
		}
		/*STRING_LOAD*/
		return null;
	}
	
	/**
	 * msg[0] contains the name of the message
	 * for any i being an odd number,
	 * msg[i] contains the name of the param
	 * msg[i+1] contains the value of the i/2th param
	**/
	private Event parse(Port port, String[] msg) {//TODO: to be generated
		if (msg.length != 3) //1 (for message + +2 for each param)
			return null;
		if ("timer_start".equals(msg[0])) {
			if ("delay".equals(msg[1])) {
				short delay = Short.parseShort(msg[2]);
				return instantiate(port, delay);
			}			
		}
		return null;
	}

	public class Timer_startMessage extends Event implements java.io.Serializable {//no change here

		public final short delay;
		
		protected Timer_startMessage(EventType type, Port port, final short delay) {
			super(type, port);
			this.delay = delay;
		}		
		
		@ Override
		public Event clone() {
			return instantiate(getPort(), this.delay);
		}
		
		@ Override
		public String toString() {
			return "timer_start (" + "delay: " + delay + ")";
		}			
	}
	
	public String toString(Timer_startMessage event, String serialization) {//TODO: to be generated
		if (serialization == null || serialization.equals("default")) {
			return event.toString();
		} else if (serialization.equals("json-default")) {
			return "{\"timer_start\":{" + "\"delay\": " + event.delay + "}}";
		}
		/*STRING_SAVE*/
		return null;
	}
				
	public byte[] toBytes(Timer_startMessage event, String serialization) {//TODO: to be generated
		if (serialization == null || serialization.equals("default")) {
			ByteBuffer buffer = ByteBuffer.allocate(1+2);
			buffer.order(ByteOrder.BIG_ENDIAN);
			buffer.put(code);
			buffer.putShort(event.delay);
			return buffer.array();
		}
		/*BINARY_SAVE*/
		return null;
	}		
}
Clone this wiki locally