Skip to content

Commit 361c690

Browse files
authored
Merge pull request #1 from mgatny/master
Examples in various languages
2 parents 97af467 + 8fe98f0 commit 361c690

File tree

12 files changed

+419
-1
lines changed

12 files changed

+419
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
java/target/
2+
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](go/)
22+
- [Java](java/)
23+
- [Javascript](javascript/)
24+
- [Ruby](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: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
Header struct {
21+
BeginString string
22+
MsgType string
23+
MsgSeqNum string
24+
SenderCompID string
25+
TargetCompID string
26+
SendingTime string
27+
}
28+
29+
Body struct {
30+
SecurityIDSource string
31+
SecurityID string
32+
MDReqID string
33+
NoMDEntries []NoMDEntries
34+
}
35+
36+
Trailer struct {
37+
}
38+
}
39+
40+
var (
41+
data = []byte(`{
42+
"Header": {
43+
"BeginString": "FIXT.1.1",
44+
"MsgType": "W",
45+
"MsgSeqNum": "4567",
46+
"SenderCompID": "SENDER",
47+
"TargetCompID": "TARGET",
48+
"SendingTime": "20160802-21:14:38.717"
49+
},
50+
"Body": {
51+
"SecurityIDSource": "8",
52+
"SecurityID": "ESU6",
53+
"MDReqID": "789",
54+
"NoMDEntries": [
55+
{ "MDEntryType": "0", "MDEntryPx": "2179.75", "MDEntrySize": "175", "MDEntryDate": "20160812", "MDEntryTime": "21:14:38.688"},
56+
{ "MDEntryType": "1", "MDEntryPx": "2180.25", "MDEntrySize": "125", "MDEntryDate": "20160812", "MDEntryTime": "21:14:38.688"}
57+
]
58+
},
59+
"Trailer": {
60+
}
61+
}`)
62+
)
63+
64+
func TestJSON(t *testing.T) {
65+
// When the JSON is parsed
66+
var msg MarketDataSnapshotFullRefresh
67+
err := json.Unmarshal(data, &msg)
68+
require.Nil(t, err)
69+
70+
// Then the header fields should be
71+
assert.Equal(t, "FIXT.1.1", msg.Header.BeginString)
72+
assert.Equal(t, "W", msg.Header.MsgType)
73+
assert.Equal(t, "4567", msg.Header.MsgSeqNum)
74+
assert.Equal(t, "SENDER", msg.Header.SenderCompID)
75+
assert.Equal(t, "TARGET", msg.Header.TargetCompID)
76+
assert.Equal(t, "20160802-21:14:38.717", msg.Header.SendingTime)
77+
78+
// And the body fields should be
79+
assert.Equal(t, "8", msg.Body.SecurityIDSource)
80+
assert.Equal(t, "ESU6", msg.Body.SecurityID)
81+
assert.Equal(t, "789", msg.Body.MDReqID)
82+
83+
// And the NoMDEntries repeating group should contain two entries
84+
assert.Len(t, msg.Body.NoMDEntries, 2)
85+
86+
// And the first entry should be
87+
assert.Equal(t, "0", msg.Body.NoMDEntries[0].MDEntryType)
88+
assert.Equal(t, "2179.75", msg.Body.NoMDEntries[0].MDEntryPx)
89+
assert.Equal(t, "175", msg.Body.NoMDEntries[0].MDEntrySize)
90+
assert.Equal(t, "20160812", msg.Body.NoMDEntries[0].MDEntryDate)
91+
assert.Equal(t, "21:14:38.688", msg.Body.NoMDEntries[0].MDEntryTime)
92+
93+
// And the second entry should be
94+
assert.Equal(t, "1", msg.Body.NoMDEntries[1].MDEntryType)
95+
assert.Equal(t, "2180.25", msg.Body.NoMDEntries[1].MDEntryPx)
96+
assert.Equal(t, "125", msg.Body.NoMDEntries[1].MDEntrySize)
97+
assert.Equal(t, "20160812", msg.Body.NoMDEntries[1].MDEntryDate)
98+
assert.Equal(t, "21:14:38.688", msg.Body.NoMDEntries[1].MDEntryTime)
99+
}

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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 Header {
10+
String BeginString;
11+
String MsgType;
12+
String MsgSeqNum;
13+
String SenderCompID;
14+
String TargetCompID;
15+
String SendingTime;
16+
}
17+
18+
static class Trailer {
19+
}
20+
21+
static class NoMDEntries {
22+
String MDEntryType;
23+
String MDEntryPx;
24+
String MDEntrySize;
25+
String MDEntryDate;
26+
String MDEntryTime;
27+
}
28+
29+
static class MarketDataSnapshotFullRefresh {
30+
static class Body {
31+
String SecurityIDSource;
32+
String SecurityID;
33+
String MDReqID;
34+
NoMDEntries[] NoMDEntries;
35+
}
36+
37+
Header Header;
38+
Body Body;
39+
Trailer Trailer;
40+
}
41+
42+
private final static String data = String.join("\n",
43+
"{",
44+
"\"Header\": {",
45+
"\"BeginString\": \"FIXT.1.1\",",
46+
"\"MsgType\": \"W\",",
47+
"\"MsgSeqNum\": \"4567\",",
48+
"\"SenderCompID\": \"SENDER\",",
49+
"\"TargetCompID\": \"TARGET\",",
50+
"\"SendingTime\": \"20160802-21:14:38.717\"",
51+
"},",
52+
"\"Body\": {",
53+
"\"SecurityIDSource\": \"8\",",
54+
"\"SecurityID\": \"ESU6\",",
55+
"\"MDReqID\": \"789\",",
56+
"\"NoMDEntries\": [",
57+
"{ \"MDEntryType\": \"0\", \"MDEntryPx\": \"2179.75\", \"MDEntrySize\": \"175\", \"MDEntryDate\": \"20160812\", \"MDEntryTime\": \"21:14:38.688\"},",
58+
"{ \"MDEntryType\": \"1\", \"MDEntryPx\": \"2180.25\", \"MDEntrySize\": \"125\", \"MDEntryDate\": \"20160812\", \"MDEntryTime\": \"21:14:38.688\"}",
59+
"]",
60+
"},",
61+
"\"Trailer\": {",
62+
"}",
63+
"}"
64+
);
65+
66+
@Test
67+
public void testJson() {
68+
// When the JSON is parsed
69+
MarketDataSnapshotFullRefresh msg = new Gson().fromJson(data, MarketDataSnapshotFullRefresh.class);
70+
71+
// Then the header fields should be
72+
assertEquals("FIXT.1.1", msg.Header.BeginString);
73+
assertEquals("W", msg.Header.MsgType);
74+
assertEquals("4567", msg.Header.MsgSeqNum);
75+
assertEquals("SENDER", msg.Header.SenderCompID);
76+
assertEquals("TARGET", msg.Header.TargetCompID);
77+
assertEquals("20160802-21:14:38.717", msg.Header.SendingTime);
78+
79+
// And the body fields should be
80+
assertEquals("8", msg.Body.SecurityIDSource);
81+
assertEquals("ESU6", msg.Body.SecurityID);
82+
assertEquals("789", msg.Body.MDReqID);
83+
84+
// And the NoMDEntries repeating group should contain two entries
85+
assertEquals(2, msg.Body.NoMDEntries.length);
86+
87+
// And the first entry should be
88+
assertEquals("0", msg.Body.NoMDEntries[0].MDEntryType);
89+
assertEquals("2179.75", msg.Body.NoMDEntries[0].MDEntryPx);
90+
assertEquals("175", msg.Body.NoMDEntries[0].MDEntrySize);
91+
assertEquals("20160812", msg.Body.NoMDEntries[0].MDEntryDate);
92+
assertEquals("21:14:38.688", msg.Body.NoMDEntries[0].MDEntryTime);
93+
94+
// And the second entry should be
95+
assertEquals("1", msg.Body.NoMDEntries[1].MDEntryType);
96+
assertEquals("2180.25", msg.Body.NoMDEntries[1].MDEntryPx);
97+
assertEquals("125", msg.Body.NoMDEntries[1].MDEntrySize);
98+
assertEquals("20160812", msg.Body.NoMDEntries[1].MDEntryDate);
99+
assertEquals("21:14:38.688", msg.Body.NoMDEntries[1].MDEntryTime);
100+
}
101+
}

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