Skip to content

Commit

Permalink
Merge pull request #1 from mgatny/master
Browse files Browse the repository at this point in the history
Examples in various languages
  • Loading branch information
mgatny authored Sep 12, 2016
2 parents 97af467 + 8fe98f0 commit 361c690
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
java/target/
scratch/
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# fix-json-encoding

## License

FIX JSON Encoding is © Copyright 2016 FIX Protocol Limited.

Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,4 +14,12 @@ Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.

## Examples

- [Go](go/)
- [Java](java/)
- [Javascript](javascript/)
- [Ruby](ruby/)

7 changes: 7 additions & 0 deletions go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Go (golang) Reference Code

Example of parsing FIX JSON Encoding in Go. Run it with:

```shell
go test
```
99 changes: 99 additions & 0 deletions go/msg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package msgtest

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type NoMDEntries struct {
MDEntryType string
MDEntryPx string
MDEntrySize string
MDEntryDate string
MDEntryTime string
}

type MarketDataSnapshotFullRefresh struct {
Header struct {
BeginString string
MsgType string
MsgSeqNum string
SenderCompID string
TargetCompID string
SendingTime string
}

Body struct {
SecurityIDSource string
SecurityID string
MDReqID string
NoMDEntries []NoMDEntries
}

Trailer struct {
}
}

var (
data = []byte(`{
"Header": {
"BeginString": "FIXT.1.1",
"MsgType": "W",
"MsgSeqNum": "4567",
"SenderCompID": "SENDER",
"TargetCompID": "TARGET",
"SendingTime": "20160802-21:14:38.717"
},
"Body": {
"SecurityIDSource": "8",
"SecurityID": "ESU6",
"MDReqID": "789",
"NoMDEntries": [
{ "MDEntryType": "0", "MDEntryPx": "2179.75", "MDEntrySize": "175", "MDEntryDate": "20160812", "MDEntryTime": "21:14:38.688"},
{ "MDEntryType": "1", "MDEntryPx": "2180.25", "MDEntrySize": "125", "MDEntryDate": "20160812", "MDEntryTime": "21:14:38.688"}
]
},
"Trailer": {
}
}`)
)

func TestJSON(t *testing.T) {
// When the JSON is parsed
var msg MarketDataSnapshotFullRefresh
err := json.Unmarshal(data, &msg)
require.Nil(t, err)

// Then the header fields should be
assert.Equal(t, "FIXT.1.1", msg.Header.BeginString)
assert.Equal(t, "W", msg.Header.MsgType)
assert.Equal(t, "4567", msg.Header.MsgSeqNum)
assert.Equal(t, "SENDER", msg.Header.SenderCompID)
assert.Equal(t, "TARGET", msg.Header.TargetCompID)
assert.Equal(t, "20160802-21:14:38.717", msg.Header.SendingTime)

// And the body fields should be
assert.Equal(t, "8", msg.Body.SecurityIDSource)
assert.Equal(t, "ESU6", msg.Body.SecurityID)
assert.Equal(t, "789", msg.Body.MDReqID)

// And the NoMDEntries repeating group should contain two entries
assert.Len(t, msg.Body.NoMDEntries, 2)

// And the first entry should be
assert.Equal(t, "0", msg.Body.NoMDEntries[0].MDEntryType)
assert.Equal(t, "2179.75", msg.Body.NoMDEntries[0].MDEntryPx)
assert.Equal(t, "175", msg.Body.NoMDEntries[0].MDEntrySize)
assert.Equal(t, "20160812", msg.Body.NoMDEntries[0].MDEntryDate)
assert.Equal(t, "21:14:38.688", msg.Body.NoMDEntries[0].MDEntryTime)

// And the second entry should be
assert.Equal(t, "1", msg.Body.NoMDEntries[1].MDEntryType)
assert.Equal(t, "2180.25", msg.Body.NoMDEntries[1].MDEntryPx)
assert.Equal(t, "125", msg.Body.NoMDEntries[1].MDEntrySize)
assert.Equal(t, "20160812", msg.Body.NoMDEntries[1].MDEntryDate)
assert.Equal(t, "21:14:38.688", msg.Body.NoMDEntries[1].MDEntryTime)
}
7 changes: 7 additions & 0 deletions java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Java Reference Code

Example of parsing FIX JSON Encoding in Java. Run it with:

```shell
mvn test
```
57 changes: 57 additions & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.fixtradingcommunity.fix-examples</groupId>
<artifactId>json-msg-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<description>FIX/JSON encoding spike</description>
<inceptionYear>2016</inceptionYear>

<organization>
<name>FIX Trading Community</name>
<url>http://http://www.fixtradingcommunity.org/</url>
</organization>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.11</junit.version>
<java.version>1.8</java.version>
<gson.version>2.6.2</gson.version>
</properties>

<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>${junit.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
101 changes: 101 additions & 0 deletions java/src/test/java/org/fixtradingcommunity/MsgTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.fixtradingcommunity;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.Before;
import com.google.gson.*;

public class MsgTest {
static class Header {
String BeginString;
String MsgType;
String MsgSeqNum;
String SenderCompID;
String TargetCompID;
String SendingTime;
}

static class Trailer {
}

static class NoMDEntries {
String MDEntryType;
String MDEntryPx;
String MDEntrySize;
String MDEntryDate;
String MDEntryTime;
}

static class MarketDataSnapshotFullRefresh {
static class Body {
String SecurityIDSource;
String SecurityID;
String MDReqID;
NoMDEntries[] NoMDEntries;
}

Header Header;
Body Body;
Trailer Trailer;
}

private final static String data = String.join("\n",
"{",
"\"Header\": {",
"\"BeginString\": \"FIXT.1.1\",",
"\"MsgType\": \"W\",",
"\"MsgSeqNum\": \"4567\",",
"\"SenderCompID\": \"SENDER\",",
"\"TargetCompID\": \"TARGET\",",
"\"SendingTime\": \"20160802-21:14:38.717\"",
"},",
"\"Body\": {",
"\"SecurityIDSource\": \"8\",",
"\"SecurityID\": \"ESU6\",",
"\"MDReqID\": \"789\",",
"\"NoMDEntries\": [",
"{ \"MDEntryType\": \"0\", \"MDEntryPx\": \"2179.75\", \"MDEntrySize\": \"175\", \"MDEntryDate\": \"20160812\", \"MDEntryTime\": \"21:14:38.688\"},",
"{ \"MDEntryType\": \"1\", \"MDEntryPx\": \"2180.25\", \"MDEntrySize\": \"125\", \"MDEntryDate\": \"20160812\", \"MDEntryTime\": \"21:14:38.688\"}",
"]",
"},",
"\"Trailer\": {",
"}",
"}"
);

@Test
public void testJson() {
// When the JSON is parsed
MarketDataSnapshotFullRefresh msg = new Gson().fromJson(data, MarketDataSnapshotFullRefresh.class);

// Then the header fields should be
assertEquals("FIXT.1.1", msg.Header.BeginString);
assertEquals("W", msg.Header.MsgType);
assertEquals("4567", msg.Header.MsgSeqNum);
assertEquals("SENDER", msg.Header.SenderCompID);
assertEquals("TARGET", msg.Header.TargetCompID);
assertEquals("20160802-21:14:38.717", msg.Header.SendingTime);

// And the body fields should be
assertEquals("8", msg.Body.SecurityIDSource);
assertEquals("ESU6", msg.Body.SecurityID);
assertEquals("789", msg.Body.MDReqID);

// And the NoMDEntries repeating group should contain two entries
assertEquals(2, msg.Body.NoMDEntries.length);

// And the first entry should be
assertEquals("0", msg.Body.NoMDEntries[0].MDEntryType);
assertEquals("2179.75", msg.Body.NoMDEntries[0].MDEntryPx);
assertEquals("175", msg.Body.NoMDEntries[0].MDEntrySize);
assertEquals("20160812", msg.Body.NoMDEntries[0].MDEntryDate);
assertEquals("21:14:38.688", msg.Body.NoMDEntries[0].MDEntryTime);

// And the second entry should be
assertEquals("1", msg.Body.NoMDEntries[1].MDEntryType);
assertEquals("2180.25", msg.Body.NoMDEntries[1].MDEntryPx);
assertEquals("125", msg.Body.NoMDEntries[1].MDEntrySize);
assertEquals("20160812", msg.Body.NoMDEntries[1].MDEntryDate);
assertEquals("21:14:38.688", msg.Body.NoMDEntries[1].MDEntryTime);
}
}
7 changes: 7 additions & 0 deletions javascript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Javascript Reference Code

Example of parsing FIX JSON Encoding in Javascript. Run it with:

```shell
node msg_test.js
```
56 changes: 56 additions & 0 deletions javascript/msg_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const assert = require('assert');

const data = '{' +
' "Header": {' +
' "BeginString": "FIXT.1.1",' +
' "MsgType": "W",' +
' "MsgSeqNum": "4567",' +
' "SenderCompID": "SENDER",' +
' "TargetCompID": "TARGET",' +
' "SendingTime": "20160802-21:14:38.717"' +
' },' +
' "Body": {' +
' "SecurityIDSource": "8",' +
' "SecurityID": "ESU6",' +
' "MDReqID": "789",' +
' "NoMDEntries": [' +
' { "MDEntryType": "0", "MDEntryPx": "2179.75", "MDEntrySize": "175", "MDEntryDate": "20160812", "MDEntryTime": "21:14:38.688"},' +
' { "MDEntryType": "1", "MDEntryPx": "2180.25", "MDEntrySize": "125", "MDEntryDate": "20160812", "MDEntryTime": "21:14:38.688"}' +
' ]' +
' },' +
' "Trailer": {' +
' }' +
'}';

// When the JSON is parsed
var msg = JSON.parse(data);

// Then the header fields should be
assert.equal(msg.Header.BeginString, "FIXT.1.1");
assert.equal(msg.Header.MsgType, "W");
assert.equal(msg.Header.MsgSeqNum, "4567");
assert.equal(msg.Header.SenderCompID, "SENDER");
assert.equal(msg.Header.TargetCompID, "TARGET");
assert.equal(msg.Header.SendingTime, "20160802-21:14:38.717");

// And the body fields should be
assert.equal(msg.Body.SecurityIDSource, "8");
assert.equal(msg.Body.SecurityID, "ESU6");
assert.equal(msg.Body.MDReqID, "789");

// And the NoMDEntries repeating group should contain two entries
assert.equal(msg.Body.NoMDEntries.length, 2);

// And the first entry should be
assert.equal(msg.Body.NoMDEntries[0].MDEntryType, "0");
assert.equal(msg.Body.NoMDEntries[0].MDEntryPx, "2179.75");
assert.equal(msg.Body.NoMDEntries[0].MDEntrySize, "175");
assert.equal(msg.Body.NoMDEntries[0].MDEntryDate, "20160812");
assert.equal(msg.Body.NoMDEntries[0].MDEntryTime, "21:14:38.688");

// And the second entry should be
assert.equal(msg.Body.NoMDEntries[1].MDEntryType, "1");
assert.equal(msg.Body.NoMDEntries[1].MDEntryPx, "2180.25");
assert.equal(msg.Body.NoMDEntries[1].MDEntrySize, "125");
assert.equal(msg.Body.NoMDEntries[1].MDEntryDate, "20160812");
assert.equal(msg.Body.NoMDEntries[1].MDEntryTime, "21:14:38.688");
1 change: 1 addition & 0 deletions ruby/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.2.2
7 changes: 7 additions & 0 deletions ruby/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Ruby Reference Code

Example of parsing FIX JSON Encoding in Ruby. Run it with:

```shell
ruby msg_test.rb
```
Loading

0 comments on commit 361c690

Please sign in to comment.