Skip to content

Instantly share code, notes, and snippets.

@cherrera2001
Created November 26, 2018 18:00
Show Gist options
  • Save cherrera2001/d39b944640de1202d91ac1a9203d525c to your computer and use it in GitHub Desktop.
Save cherrera2001/d39b944640de1202d91ac1a9203d525c to your computer and use it in GitHub Desktop.
Convert JSON to WITSML XML
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hashmapinc.tempus.WitsmlObjects.v1411.ObjWell;
import java.io.IOException;
public class Converter {
public static ObjWell convert(String json) throws IOException {
ObjWell well = new ObjectMapper().readValue(json, ObjWell.class);
return well;
}
}
import com.hashmapinc.tempus.WitsmlObjects.v1411.ObjWell;
import com.hashmapinc.tempus.WitsmlObjects.v1411.ObjWells;
import org.junit.Assert;
import org.junit.Test;
import javax.xml.bind.JAXBException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Objects;
import java.util.Scanner;
public class ConverterTest {
@Test
public void shouldParseHuge() throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, JAXBException {
String data = getData("WmlTest1.json");
ObjWell well = Converter.convert(data);
ObjWells wells = new ObjWells();
wells.getWell().add(well);
System.out.println(com.hashmapinc.tempus.WitsmlObjects.Util.WitsmlMarshal.serialize(wells));
Assert.assertNotNull(well);
}
@Test
public void shouldParseSmall() throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, JAXBException {
String data = getData("WmlTest2.json");
ObjWell well = Converter.convert(data);
ObjWells wells = new ObjWells();
wells.getWell().add(well);
System.out.println(com.hashmapinc.tempus.WitsmlObjects.Util.WitsmlMarshal.serialize(wells));
Assert.assertNotNull(well);
}
private String getData(String fileName) {
StringBuilder result = new StringBuilder();
//Get file from resources folder
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(Objects.requireNonNull(classLoader.getResource(fileName)).getFile());
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
result.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
}
{
"name": "string",
"nameLegal": "string",
"numLicense": "string",
"numGovt": "string",
"field": "string",
"country": "string",
"state": "string",
"county": "string",
"region": "string",
"district": "string",
"block": "string",
"timeZone": "string",
"operator": "string",
"operatorDiv": "string",
"pcInterest": {
"uom": "string",
"value": 0
},
"numAPI": "string",
"statusWell": "WORKING_OVER",
"purposeWell": "GENERAL_SRVC",
"fluidWell": "WATER_BRINE",
"directionWell": "HUFF_N_PUFF",
"wellheadElevation": {
"datum": "string",
"uom": "FT",
"value": 0
},
"groundElevation": {
"datum": "string",
"uom": "FT",
"value": 0
},
"waterDepth": {
"uom": "string",
"value": 0
},
"wellLocation": [
{
"uid": "string",
"original": true,
"description": "string",
"extensionNameValue": [
{
"name": "string",
"value": {
"uom": "string",
"value": "string"
},
"measureClass": "string",
"index": 0,
"description": "string",
"md": {
"datum": "string",
"uom": "FT",
"value": 0
},
"dataType": "DURATION"
}
]
}
],
"wellPublicLandSurveySystemLocation": {
"principalMeridian": "string",
"range": 0,
"rangeDir": "UNKNOWN",
"township": 0,
"townshipDir": "UNKNOWN",
"section": "string",
"quarterSection": "string",
"quarterTownship": "string",
"footageNS": {
"uom": "string",
"value": 0
},
"footageEW": {
"uom": "string",
"value": 0
}
},
"referencePoint": [
{
"name": "string",
"type": "string",
"measuredDepth": {
"datum": "string",
"uom": "FT",
"value": 0
},
"description": "string",
"extensionNameValue": [
{
"name": "string",
"value": {
"uom": "string",
"value": "string"
},
"measureClass": "string",
"index": 0,
"description": "string",
"md": {
"datum": "string",
"uom": "FT",
"value": 0
},
"dataType": "UNKNOWN"
}
],
"elevation": {
"datum": "string",
"uom": "FT",
"value": 0
},
"location": [
{
"uid": "string",
"original": true,
"description": "string",
"extensionNameValue": [
{
"name": "string",
"value": {
"uom": "string",
"value": "string"
},
"measureClass": "string",
"index": 0,
"description": "string",
"md": {
"datum": "string",
"uom": "FT",
"value": 0
},
"dataType": "UNKNOWN"
}
]
}
],
"uid": "string"
}
],
"wellCRS": [
{
"name": "string",
"mapProjection": {
"nameCRS": {
"namingSystem": "string",
"code": "string",
"value": "string"
},
"projectionCode": "UNKNOWN",
"projectedFrom": {
"uidRef": "string",
"value": "string"
},
"stdParallel1": {
"uom": "string",
"value": 0
},
"stdParallel2": {
"uom": "string",
"value": 0
},
"centralMeridian": {
"uom": "string",
"value": 0
},
"originLatitude": {
"uom": "string",
"value": 0
},
"originLongitude": {
"uom": "string",
"value": 0
},
"latitude1": {
"uom": "string",
"value": 0
},
"longitude1": {
"uom": "string",
"value": 0
},
"latitude2": {
"uom": "string",
"value": 0
},
"longitude2": {
"uom": "string",
"value": 0
},
"latitudeForScale": {
"uom": "string",
"value": 0
},
"longitudeForScale": {
"uom": "string",
"value": 0
},
"trueScaleLatitude": {
"uom": "string",
"value": 0
},
"spheroidRadius": {
"uom": "string",
"value": 0
},
"scaleFactor": 0,
"methodVariant": "UNKNOWN",
"perspectiveHeight": {
"uom": "string",
"value": 0
},
"zone": "string",
"falseEasting": {
"uom": "string",
"value": 0
},
"falseNorthing": {
"uom": "string",
"value": 0
},
"bearing": {
"uom": "string",
"value": 0
},
"hemisphere": "UNKNOWN",
"description": "string",
"parameter": [
{
"index": 0,
"name": "string",
"uom": "string",
"description": "string",
"uid": "string",
"value": "string"
}
]
},
"geographic": {
"nameCRS": {
"namingSystem": "string",
"code": "string",
"value": "string"
},
"geodeticDatumCode": "NAD_27",
"scaleFactor": 0,
"ellipsoidCode": "UNKNOWN",
"ellipsoidSemiMajorAxis": {
"uom": "string",
"value": 0
},
"ellipsoidInverseFlattening": 0
},
"mapProjectionCRS": {
"uidRef": "string",
"value": "string"
},
"geodeticCRS": {
"uidRef": "string",
"value": "string"
},
"localCRS": {
"usesWellAsOrigin": true,
"origin": {
"uidRef": "string",
"value": "string"
},
"originDescription": "string",
"magneticDeclination": {
"uom": "string",
"value": 0
},
"gridConvergence": {
"uom": "string",
"value": 0
}
},
"description": "string",
"extensionNameValue": [
{
"name": "string",
"value": {
"uom": "string",
"value": "string"
},
"measureClass": "string",
"index": 0,
"description": "string",
"md": {
"datum": "string",
"uom": "FT",
"value": 0
},
"dataType": "UNKNOWN"
}
],
"uid": "string"
}
],
"commonData": {
"sourceName": "string",
"itemState": "UNKNOWN",
"serviceCategory": "string",
"comments": "string",
"acquisitionTimeZone": [
{
"value": "string"
}
],
"defaultDatum": {
"uidRef": "string",
"value": "string"
},
"privateGroupOnly": true,
"extensionNameValue": [
{
"name": "string",
"value": {
"uom": "string",
"value": "string"
},
"measureClass": "string",
"index": 0,
"description": "string",
"md": {
"datum": "string",
"uom": "FT",
"value": 0
},
"dataType": "UNKNOWN"
}
]
},
"wellDatum": [
{
"name": "string",
"code": "DF",
"datumName": {
"namingSystem": "string",
"code": "string",
"value": "string"
},
"datumCRS": {
"uidRef": "string",
"value": "string"
},
"kind": [
"string"
],
"wellbore": {
"wellboreReference": {
"uidRef": "string",
"value": "string"
},
"wellParent": {
"uidRef": "string",
"value": "string"
}
},
"rig": {
"rigReference": {
"uidRef": "string",
"value": "string"
},
"wellboreParent": {
"uidRef": "string",
"value": "string"
},
"wellParent": {
"uidRef": "string",
"value": "string"
}
},
"elevation": {
"datum": "string",
"uom": "FT",
"value": 0
},
"measuredDepth": {
"datum": "string",
"uom": "FT",
"value": 0
},
"horizontalLocation": {
"wellCRS": {
"uidRef": "string",
"value": "string"
},
"latitude": {
"uom": "string",
"value": 0
},
"longitude": {
"uom": "string",
"value": 0
},
"easting": {
"uom": "string",
"value": 0
},
"northing": {
"uom": "string",
"value": 0
},
"westing": {
"uom": "string",
"value": 0
},
"southing": {
"uom": "string",
"value": 0
},
"projectedX": {
"uom": "string",
"value": 0
},
"projectedY": {
"uom": "string",
"value": 0
},
"localX": {
"uom": "string",
"value": 0
},
"localY": {
"uom": "string",
"value": 0
},
"original": true,
"description": "string",
"extensionNameValue": [
{
"name": "string",
"value": {
"uom": "string",
"value": "string"
},
"measureClass": "string",
"index": 0,
"description": "string",
"md": {
"datum": "string",
"uom": "FT",
"value": 0
},
"dataType": "UNKNOWN"
}
],
"uid": "string"
},
"comment": "string",
"extensionNameValue": [
{
"name": "string",
"value": {
"uom": "string",
"value": "string"
},
"measureClass": "string",
"index": 0,
"description": "string",
"md": {
"datum": "string",
"uom": "FT",
"value": 0
},
"dataType": "UNKNOWN"
}
],
"uid": "string"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment