Skip to content

Commit 5cd0198

Browse files
committed
Examples in various languages
1 parent 97af467 commit 5cd0198

12 files changed

Lines changed: 374 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scratch/

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# fix-json-encoding
22

33
## License
4+
45
FIX JSON Encoding is © Copyright 2016 FIX Protocol Limited.
56

67
Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,4 +14,12 @@ Unless required by applicable law or agreed to in writing, software
1314
distributed under the License is distributed on an "AS IS" BASIS,
1415
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1516
See the License for the specific language governing permissions and
16-
limitations under the License.
17+
limitations under the License.
18+
19+
## Examples
20+
21+
- [Go](blob/master/go)
22+
- [Java](blob/master/java)
23+
- [Javascript](blob/master/javascript)
24+
- [Ruby](blob/master/ruby)
25+

go/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Go (golang) Reference Code
2+
3+
Example of parsing FIX JSON Encoding in Go. Run it with:
4+
5+
```shell
6+
go test
7+
```

go/msg_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package msgtest
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
type NoMDEntries struct {
12+
MDEntryType string
13+
MDEntryPx string
14+
MDEntrySize string
15+
MDEntryDate string
16+
MDEntryTime string
17+
}
18+
19+
type MarketDataSnapshotFullRefresh struct {
20+
BeginString string
21+
MsgType string
22+
MsgSeqNum string
23+
SenderCompID string
24+
TargetCompID string
25+
SendingTime string
26+
SecurityIDSource string
27+
SecurityID string
28+
MDReqID string
29+
NoMDEntries []NoMDEntries
30+
}
31+
32+
var (
33+
data = []byte(`{
34+
"BeginString": "FIXT.1.1",
35+
"MsgType": "W",
36+
"MsgSeqNum": "4567",
37+
"SenderCompID": "SENDER",
38+
"TargetCompID": "TARGET",
39+
"SendingTime": "20160802-21:14:38.717",
40+
"SecurityIDSource": "8",
41+
"SecurityID": "ESU6",
42+
"MDReqID": "789",
43+
"NoMDEntries": [
44+
{ "MDEntryType": "0", "MDEntryPx": "2179.75", "MDEntrySize": "175", "MDEntryDate": "20160812", "MDEntryTime": "21:14:38.688"},
45+
{ "MDEntryType": "1", "MDEntryPx": "2180.25", "MDEntrySize": "125", "MDEntryDate": "20160812", "MDEntryTime": "21:14:38.688"}
46+
]
47+
}`)
48+
)
49+
50+
func TestJSON(t *testing.T) {
51+
// When the JSON is parsed
52+
var msg MarketDataSnapshotFullRefresh
53+
err := json.Unmarshal(data, &msg)
54+
require.Nil(t, err)
55+
56+
// Then the header fields should be
57+
assert.Equal(t, "FIXT.1.1", msg.BeginString)
58+
assert.Equal(t, "W", msg.MsgType)
59+
assert.Equal(t, "4567", msg.MsgSeqNum)
60+
assert.Equal(t, "SENDER", msg.SenderCompID)
61+
assert.Equal(t, "TARGET", msg.TargetCompID)
62+
assert.Equal(t, "20160802-21:14:38.717", msg.SendingTime)
63+
64+
// And the body fields should be
65+
assert.Equal(t, "8", msg.SecurityIDSource)
66+
assert.Equal(t, "ESU6", msg.SecurityID)
67+
assert.Equal(t, "789", msg.MDReqID)
68+
69+
// And the NoMDEntries repeating group should contain two entries
70+
assert.Len(t, msg.NoMDEntries, 2)
71+
72+
// And the first entry should be
73+
assert.Equal(t, "0", msg.NoMDEntries[0].MDEntryType)
74+
assert.Equal(t, "2179.75", msg.NoMDEntries[0].MDEntryPx)
75+
assert.Equal(t, "175", msg.NoMDEntries[0].MDEntrySize)
76+
assert.Equal(t, "20160812", msg.NoMDEntries[0].MDEntryDate)
77+
assert.Equal(t, "21:14:38.688", msg.NoMDEntries[0].MDEntryTime)
78+
79+
// And the second entry should be
80+
assert.Equal(t, "1", msg.NoMDEntries[1].MDEntryType)
81+
assert.Equal(t, "2180.25", msg.NoMDEntries[1].MDEntryPx)
82+
assert.Equal(t, "125", msg.NoMDEntries[1].MDEntrySize)
83+
assert.Equal(t, "20160812", msg.NoMDEntries[1].MDEntryDate)
84+
assert.Equal(t, "21:14:38.688", msg.NoMDEntries[1].MDEntryTime)
85+
}

java/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Java Reference Code
2+
3+
Example of parsing FIX JSON Encoding in Java. Run it with:
4+
5+
```shell
6+
mvn test
7+
```

java/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.fixtradingcommunity.fix-examples</groupId>
5+
<artifactId>json-msg-test</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<name>${project.groupId}:${project.artifactId}</name>
9+
<description>FIX/JSON encoding spike</description>
10+
<inceptionYear>2016</inceptionYear>
11+
12+
<organization>
13+
<name>FIX Trading Community</name>
14+
<url>http://http://www.fixtradingcommunity.org/</url>
15+
</organization>
16+
<licenses>
17+
<license>
18+
<name>The Apache License, Version 2.0</name>
19+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
20+
</license>
21+
</licenses>
22+
23+
<properties>
24+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25+
<junit.version>4.11</junit.version>
26+
<java.version>1.8</java.version>
27+
<gson.version>2.6.2</gson.version>
28+
</properties>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>com.google.code.gson</groupId>
33+
<artifactId>gson</artifactId>
34+
<version>${gson.version}</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
<scope>test</scope>
40+
<version>${junit.version}</version>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-compiler-plugin</artifactId>
49+
<version>3.3</version>
50+
<configuration>
51+
<source>${java.version}</source>
52+
<target>${java.version}</target>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.fixtradingcommunity;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import org.junit.Test;
5+
import org.junit.Before;
6+
import com.google.gson.*;
7+
8+
public class MsgTest {
9+
static class NoMDEntries {
10+
String MDEntryType;
11+
String MDEntryPx;
12+
String MDEntrySize;
13+
String MDEntryDate;
14+
String MDEntryTime;
15+
}
16+
17+
static class MarketDataSnapshotFullRefresh {
18+
String BeginString;
19+
String MsgType;
20+
String MsgSeqNum;
21+
String SenderCompID;
22+
String TargetCompID;
23+
String SendingTime;
24+
String SecurityIDSource;
25+
String SecurityID;
26+
String MDReqID;
27+
NoMDEntries[] NoMDEntries;
28+
}
29+
30+
private final static String data = String.join("\n",
31+
"{",
32+
"\"BeginString\": \"FIXT.1.1\",",
33+
"\"MsgType\": \"W\",",
34+
"\"MsgSeqNum\": \"4567\",",
35+
"\"SenderCompID\": \"SENDER\",",
36+
"\"TargetCompID\": \"TARGET\",",
37+
"\"SendingTime\": \"20160802-21:14:38.717\",",
38+
"\"SecurityIDSource\": \"8\",",
39+
"\"SecurityID\": \"ESU6\",",
40+
"\"MDReqID\": \"789\",",
41+
"\"NoMDEntries\": [",
42+
"{ \"MDEntryType\": \"0\", \"MDEntryPx\": \"2179.75\", \"MDEntrySize\": \"175\", \"MDEntryDate\": \"20160812\", \"MDEntryTime\": \"21:14:38.688\"},",
43+
"{ \"MDEntryType\": \"1\", \"MDEntryPx\": \"2180.25\", \"MDEntrySize\": \"125\", \"MDEntryDate\": \"20160812\", \"MDEntryTime\": \"21:14:38.688\"}",
44+
"]",
45+
"}"
46+
);
47+
48+
@Test
49+
public void testJson() {
50+
// When the JSON is parsed
51+
MarketDataSnapshotFullRefresh msg = new Gson().fromJson(data, MarketDataSnapshotFullRefresh.class);
52+
53+
// Then the header fields should be
54+
assertEquals("FIXT.1.1", msg.BeginString);
55+
assertEquals("W", msg.MsgType);
56+
assertEquals("4567", msg.MsgSeqNum);
57+
assertEquals("SENDER", msg.SenderCompID);
58+
assertEquals("TARGET", msg.TargetCompID);
59+
assertEquals("20160802-21:14:38.717", msg.SendingTime);
60+
61+
// And the body fields should be
62+
assertEquals("8", msg.SecurityIDSource);
63+
assertEquals("ESU6", msg.SecurityID);
64+
assertEquals("789", msg.MDReqID);
65+
66+
// And the NoMDEntries repeating group should contain two entries
67+
assertEquals(2, msg.NoMDEntries.length);
68+
69+
// And the first entry should be
70+
assertEquals("0", msg.NoMDEntries[0].MDEntryType);
71+
assertEquals("2179.75", msg.NoMDEntries[0].MDEntryPx);
72+
assertEquals("175", msg.NoMDEntries[0].MDEntrySize);
73+
assertEquals("20160812", msg.NoMDEntries[0].MDEntryDate);
74+
assertEquals("21:14:38.688", msg.NoMDEntries[0].MDEntryTime);
75+
76+
// And the second entry should be
77+
assertEquals("1", msg.NoMDEntries[1].MDEntryType);
78+
assertEquals("2180.25", msg.NoMDEntries[1].MDEntryPx);
79+
assertEquals("125", msg.NoMDEntries[1].MDEntrySize);
80+
assertEquals("20160812", msg.NoMDEntries[1].MDEntryDate);
81+
assertEquals("21:14:38.688", msg.NoMDEntries[1].MDEntryTime);
82+
}
83+
}

javascript/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Javascript Reference Code
2+
3+
Example of parsing FIX JSON Encoding in Javascript. Run it with:
4+
5+
```shell
6+
node msg_test.js
7+
```

javascript/msg_test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const assert = require('assert');
2+
3+
const data = '{' +
4+
' "BeginString": "FIXT.1.1",' +
5+
' "MsgType": "W",' +
6+
' "MsgSeqNum": "4567",' +
7+
' "SenderCompID": "SENDER",' +
8+
' "TargetCompID": "TARGET",' +
9+
' "SendingTime": "20160802-21:14:38.717",' +
10+
' "SecurityIDSource": "8",' +
11+
' "SecurityID": "ESU6",' +
12+
' "MDReqID": "789",' +
13+
' "NoMDEntries": [' +
14+
' { "MDEntryType": "0", "MDEntryPx": "2179.75", "MDEntrySize": "175", "MDEntryDate": "20160812", "MDEntryTime": "21:14:38.688"},' +
15+
' { "MDEntryType": "1", "MDEntryPx": "2180.25", "MDEntrySize": "125", "MDEntryDate": "20160812", "MDEntryTime": "21:14:38.688"}' +
16+
' ]' +
17+
'}';
18+
19+
// When the JSON is parsed
20+
var msg = JSON.parse(data);
21+
22+
// Then the header fields should be
23+
assert.equal(msg.BeginString, "FIXT.1.1");
24+
assert.equal(msg.MsgType, "W");
25+
assert.equal(msg.MsgSeqNum, "4567");
26+
assert.equal(msg.SenderCompID, "SENDER");
27+
assert.equal(msg.TargetCompID, "TARGET");
28+
assert.equal(msg.SendingTime, "20160802-21:14:38.717");
29+
30+
// And the body fields should be
31+
assert.equal(msg.SecurityIDSource, "8");
32+
assert.equal(msg.SecurityID, "ESU6");
33+
assert.equal(msg.MDReqID, "789");
34+
35+
// And the NoMDEntries repeating group should contain two entries
36+
assert.equal(msg.NoMDEntries.length, 2);
37+
38+
// And the first entry should be
39+
assert.equal(msg.NoMDEntries[0].MDEntryType, "0");
40+
assert.equal(msg.NoMDEntries[0].MDEntryPx, "2179.75");
41+
assert.equal(msg.NoMDEntries[0].MDEntrySize, "175");
42+
assert.equal(msg.NoMDEntries[0].MDEntryDate, "20160812");
43+
assert.equal(msg.NoMDEntries[0].MDEntryTime, "21:14:38.688");
44+
45+
// And the second entry should be
46+
assert.equal(msg.NoMDEntries[1].MDEntryType, "1");
47+
assert.equal(msg.NoMDEntries[1].MDEntryPx, "2180.25");
48+
assert.equal(msg.NoMDEntries[1].MDEntrySize, "125");
49+
assert.equal(msg.NoMDEntries[1].MDEntryDate, "20160812");
50+
assert.equal(msg.NoMDEntries[1].MDEntryTime, "21:14:38.688");

ruby/.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.2.2

0 commit comments

Comments
 (0)