Created
March 7, 2017 04:01
-
-
Save CN6033/3711a7f55eecb63db52c9855967a336d to your computer and use it in GitHub Desktop.
FIX message to JSON convertor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.collect.ImmutableSet; | |
import com.google.common.reflect.ClassPath; | |
import com.google.gson.Gson; | |
import com.google.gson.JsonObject; | |
import quickfix.FieldNotFound; | |
import quickfix.Message; | |
import java.io.IOException; | |
import java.lang.reflect.Field; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
public class FIX2Json { | |
private static final Gson gson = new Gson(); | |
private Map<Integer, String> tagToNameMap; | |
public FIX2Json() { | |
tagToNameMap = new HashMap<>(); | |
tagToNameMap.putAll(getTagToNameMap("quickfix.field")); | |
} | |
private static Map<Integer, String> getTagToNameMap(String packageName) { | |
Map<Integer, String> tagToNameMap = new HashMap<>(); | |
try { | |
ClassPath classPath = ClassPath.from(ClassLoader.getSystemClassLoader()); | |
ImmutableSet<ClassPath.ClassInfo> classes = classPath.getTopLevelClasses(packageName); | |
for (ClassPath.ClassInfo classInfo : classes) { | |
Field field; | |
try { | |
field = classInfo.load().getField("FIELD"); | |
} catch (NoSuchFieldException e) { | |
continue; | |
} | |
int fieldTag = field.getInt(null); | |
String fieldName = classInfo.getSimpleName(); | |
tagToNameMap.put(fieldTag, fieldName); | |
} | |
} catch (IOException | IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
return tagToNameMap; | |
} | |
public String fix2Json(Message message) { | |
JsonObject jsonObject = new JsonObject(); | |
quickfix.Message.Header header = message.getHeader(); | |
Iterator<quickfix.Field<?>> headerIterator = header.iterator(); | |
while (headerIterator.hasNext()) { | |
quickfix.Field<?> field = headerIterator.next(); | |
String name = tagToNameMap.get(field.getTag()); | |
if (name == null) { | |
name = String.valueOf(field.getTag()); | |
} | |
try { | |
jsonObject.addProperty(name, header.getString(field.getTag())); | |
} catch (FieldNotFound fieldNotFound) { | |
// ignore, should not happen | |
} | |
} | |
Iterator<quickfix.Field<?>> bodyIterator = message.iterator(); | |
while (bodyIterator.hasNext()) { | |
quickfix.Field<?> field = bodyIterator.next(); | |
String name = tagToNameMap.get(field.getTag()); | |
if (name == null) { | |
name = String.valueOf(field.getTag()); | |
} | |
try { | |
jsonObject.addProperty(name, message.getString(field.getTag())); | |
} catch (FieldNotFound fieldNotFound) { | |
// ignore, should not happen | |
} | |
} | |
jsonObject.addProperty("RawData", message.toString()); | |
return gson.toJson(jsonObject); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment