From 82a9761800b24451473d0b4628396b760c70b165 Mon Sep 17 00:00:00 2001 From: Krishna Date: Mon, 7 Nov 2016 01:51:31 +0000 Subject: [PATCH] Sample Code for converting FIX to JSON --- fix2json/logs/JsonOutput.txt | 91 ++ fix2json/res/SampleMessage.txt | 4 + fix2json/res/fields.xml | 1145 +++++++++++++++++ fix2json/res/header.xml | 30 + fix2json/res/trailer.xml | 7 + .../src/org/fixtradingcommunity/fix2json.java | 168 +++ 6 files changed, 1445 insertions(+) create mode 100644 fix2json/logs/JsonOutput.txt create mode 100644 fix2json/res/SampleMessage.txt create mode 100644 fix2json/res/fields.xml create mode 100644 fix2json/res/header.xml create mode 100644 fix2json/res/trailer.xml create mode 100644 fix2json/src/org/fixtradingcommunity/fix2json.java diff --git a/fix2json/logs/JsonOutput.txt b/fix2json/logs/JsonOutput.txt new file mode 100644 index 0000000..5bd2597 --- /dev/null +++ b/fix2json/logs/JsonOutput.txt @@ -0,0 +1,91 @@ + +----------------- +{ + "Header": { + "BeginString": "FIX.4.2", + "BodyLength": "132", + "MsgType": "D", + "MsgSeqNum": "16", + "SenderCompID": "BANZAI", + "SendingTime": "20160312-20:00:05.518", + "TargetCompID": "EXEC" + }, + "Body":{ + "ClOrdID": "1457812805394", + "HandlInst": "1", + "OrderQty": "100", + "OrdType": "1", + "Side": "1", + "Symbol": "VOD.L", + "TimeInForce": "0", + "TransactTime": "20160312-20:00:05.518" + }, + "Trailer":{ + "CheckSum": "198" + } +} +----------------- +{ + "Header": { + "BeginString": "FIX.4.2", + "BodyLength": "74", + "MsgType": "A", + "MsgSeqNum": "138", + "SenderCompID": "FIXIMULATOR", + "SendingTime": "20080713-09:54:16.376", + "TargetCompID": "BANZAI" + }, + "Body":{ + "EncryptMethod": "0", + "HeartBtInt": "30" + }, + "Trailer":{ + "CheckSum": "129" + } +} +----------------- +{ + "Header": { + "BeginString": "FIX.4.2", + "BodyLength": "194", + "MsgType": "6", + "MsgSeqNum": "141", + "SenderCompID": "FIXIMULATOR", + "SendingTime": "20080713-09:54:28.295", + "TargetCompID": "BANZAI" + }, + "Body":{ + "Currency": "USD", + "IDSource": "5", + "IOIid": "I1215942868296", + "IOIShares": "61000", + "IOITransType": "N", + "Price": "27.7884", + "SecurityID": "KPA.N", + "Side": "2", + "Symbol": "KPA", + "ValidUntilTime": "20080713-10:24:28.294", + "SecurityDesc": "INNKEEPERS USA TRUST", + "IOINaturalFlag": "N" + }, + "Trailer":{ + "CheckSum": "041" + } +} +----------------- +{ + "Header": { + "BeginString": "FIX.4.2", + "BodyLength": "62", + "MsgType": "0", + "MsgSeqNum": "139", + "SenderCompID": "BANZAI", + "SendingTime": "20080713-09:54:46.592", + "TargetCompID": "FIXIMULATOR" + }, + "Body":{ + }, + "Trailer":{ + "CheckSum": "088" + } +} \ No newline at end of file diff --git a/fix2json/res/SampleMessage.txt b/fix2json/res/SampleMessage.txt new file mode 100644 index 0000000..b25f1b9 --- /dev/null +++ b/fix2json/res/SampleMessage.txt @@ -0,0 +1,4 @@ +8=FIX.4.29=13235=D34=1649=BANZAI52=20160312-20:00:05.51856=EXEC11=145781280539421=138=10040=154=155=VOD.L59=060=20160312-20:00:05.51810=198 +8=FIX.4.29=7435=A34=13849=FIXIMULATOR52=20080713-09:54:16.37656=BANZAI98=0108=3010=129 +8=FIX.4.29=19435=634=14149=FIXIMULATOR52=20080713-09:54:28.29556=BANZAI15=USD22=523=I121594286829627=6100028=N44=27.788448=KPA.N54=255=KPA62=20080713-10:24:28.294107=INNKEEPERS USA TRUST130=N10=041 +8=FIX.4.29=6235=034=13949=BANZAI52=20080713-09:54:46.59256=FIXIMULATOR10=088 diff --git a/fix2json/res/fields.xml b/fix2json/res/fields.xml new file mode 100644 index 0000000..0f4dc66 --- /dev/null +++ b/fix2json/res/fields.xml @@ -0,0 +1,1145 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/fix2json/res/header.xml b/fix2json/res/header.xml new file mode 100644 index 0000000..ef319fe --- /dev/null +++ b/fix2json/res/header.xml @@ -0,0 +1,30 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/fix2json/res/trailer.xml b/fix2json/res/trailer.xml new file mode 100644 index 0000000..5f426c5 --- /dev/null +++ b/fix2json/res/trailer.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/fix2json/src/org/fixtradingcommunity/fix2json.java b/fix2json/src/org/fixtradingcommunity/fix2json.java new file mode 100644 index 0000000..8ba574a --- /dev/null +++ b/fix2json/src/org/fixtradingcommunity/fix2json.java @@ -0,0 +1,168 @@ +package org.fixtradingcommunity; +/** +* The program converts raw FIX message into Json format +* It is not sofesticated to handle the repeating groups +* +* @author Krishna Tharnoju +* +* @version 1.0 +* @since 2016-07-11 +*/ + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.util.HashMap; +import java.util.List; +import java.util.ArrayList; + + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.DocumentBuilder; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; +import org.w3c.dom.Node; +import org.w3c.dom.Element; + + +public class fix2json { + + public static void main (String[] args) throws IOException{ + + HashMap fieldMap= new HashMap(); + List headerFields = new ArrayList(); + List trailerFields = new ArrayList(); + + try { + File inputFile = new File("res/fields.xml"); + DocumentBuilderFactory dbFactory + = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(inputFile); + doc.getDocumentElement().normalize(); + NodeList nList = doc.getElementsByTagName("field"); + for (int temp = 0; temp < nList.getLength(); temp++) { + Node nNode = nList.item(temp); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element) nNode; + fieldMap.put(eElement.getAttribute("number"), eElement.getAttribute("name")); + } + } + } catch (Exception e) { + e.printStackTrace(); + } +// System.out.println(fieldMap.toString()); + + try { + File inputFile = new File("res/header.xml"); + DocumentBuilderFactory dbFactory + = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(inputFile); + doc.getDocumentElement().normalize(); + NodeList nList = doc.getElementsByTagName("field"); + for (int temp = 0; temp < nList.getLength(); temp++) { + Node nNode = nList.item(temp); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element) nNode; + headerFields.add( eElement.getAttribute("name")); + } + + } + } catch (Exception e) { + e.printStackTrace(); + } + +// System.out.println(headerFields.toString()); + try { + File inputFile = new File("res/trailer.xml"); + DocumentBuilderFactory dbFactory + = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(inputFile); + doc.getDocumentElement().normalize(); + NodeList nList = doc.getElementsByTagName("field"); + for (int temp = 0; temp < nList.getLength(); temp++) { + Node nNode = nList.item(temp); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element) nNode; + trailerFields.add( eElement.getAttribute("name")); + } + + } + } catch (Exception e) { + e.printStackTrace(); + } +// System.out.println(trailerFields.toString()); + + FileReader fileReader = new FileReader("res/SampleMessage.txt"); + BufferedReader messageReader= new BufferedReader(fileReader ); + Writer outFile = new FileWriter("logs/JsonOutput.txt"); + BufferedWriter outputWritter= new BufferedWriter(outFile); + + + String line = null; + while((line = messageReader.readLine()) != null) { + + String jsonString = "\n-----------------\n{"; + String jsonHeader= "\n\t\"Header\": {"; + String jsonBody = "\n\t\"Body\":{"; + String jsonTrailer= "\n\t\"Trailer\":{"; + + + String[] tagPairs = line.split("\001"); + for (int i = 0; i < tagPairs.length; i++) { + + String[] tokens = tagPairs[i].split("="); + + String tagNumber = tokens[0]; + String tagName = fieldMap.get(tagNumber); + String tagValue= tokens[1]; + +// System.out.println(tagName); + + if (headerFields.contains(tagName)) + { + jsonHeader = jsonHeader + "\n\t\t\""+tagName+"\""+": \""+tagValue + "\","; + + } + else if (trailerFields.contains(tagName)) + { + jsonTrailer= jsonTrailer+ "\n\t\t\""+tagName+"\""+": \""+tagValue + "\","; + + } + else + { + jsonBody= jsonBody+ "\n\t\t\""+tagName+"\""+": \""+tagValue + "\","; + + } + } + + + jsonHeader = trimLastComma(jsonHeader) + "\n\t},"; + jsonBody = trimLastComma(jsonBody) + "\n\t},"; + jsonTrailer = trimLastComma(jsonTrailer) + "\n\t}"; + + jsonString = jsonString + jsonHeader+ jsonBody + jsonTrailer; + + jsonString = jsonString+ "\n}"; + System.out.println(jsonString ); + outputWritter.write(jsonString ); + } + outputWritter.close(); + messageReader.close(); + + } + + public static String trimLastComma(String str) { + if (str != null && str.length() > 0 && str.charAt(str.length()-1)==',') { + str = str.substring(0, str.length()-1); + } + return str; + } + +} \ No newline at end of file