Object mapping a RatesProvider object


#1

I’m trying to store an instance of a RatesProvider object in a database and I’ve tried using Object Mapping with using https://github.com/FasterXML/jackson . However due to self-references in the Rates Provider class the serialization fails.

The error is Direct self-reference leading to cycle (through reference chain: com.opengamma.strata.pricer.rate.ImmutableRatesProvider[“iborIndices”]->com.google.common.collect.SingletonImmutableSet[0]->com.opengamma.strata.basics.index.ImmutableIborIndex[“currency”]->com.opengamma.strata.basics.currency.Currency[“triangulationCurrency”])

Have you been able to store RatesProvider objects in databases? Any other classes I could use? Any suggestions?

Thank you.


#2

OP again.
Using another poster’s idea I’ve been able to store the ImmutableRatesProvider in a mongo DB using the following code:

JodaBeanSer ser = JodaBeanSer.COMPACT;
String rpAsString = ser.jsonWriter().write(ratesProvider);
DBObject irCurve = new BasicDBObject("_id", curveID);
irCurve.put(“data”,rpAsString);
WriteResult result = ratesProviders.insert(irCurve);

This creates a valid json object that looks like {"@bean":“com.opengamma.strata.pricer.rate.ImmutableRatesProvider”,“valuationDate”:“2015-07-21”,“fxRateProvider”:{"@bean":“com.opengamma.strata.data.MarketDataFxRateProvider”,“marketData”:{"@bean":“com.opengamma.strata.data.ImmutableMarketData”,“valuationDate”:"2015-07… etc

I can retrieve the DB record and convert it back into a Bean with the following code:

DBObject dbOut = ratesProviders.findOne(query);
String rpOutOfDB = (String) dbOut.get(“data”);
Bean cycled = ser.jsonReader().read(rpOutOfDB);

Is there a way of creating an ImmutableRatesProvider from the Bean?
How would I use ImmutableRatesProvider.Meta ?

Thank you.


#3

OP again - I solved my own issue. Here its the code to save a RatesProvider object in Mongo and retrieve.

ImmutableRatesProvider ratesProvider = (ImmutableRatesProvider) rp;
JodaBeanSer ser = JodaBeanSer.COMPACT;
String rpAsString = ser.jsonWriter().write(ratesProvider);
      
//Store in Mongo
BasicDBObject query = new BasicDBObject();
String curveID = "curve";
query.put("_id", curveID);
DBObject irCurve = new BasicDBObject("_id", curveID);
irCurve.put("data",rpAsString);
WriteResult result =  ratesProviders.insert(irCurve);

// Get data out of Mongo
DBObject dbOut = ratesProviders.findOne(query);
String rpOutOfDB = (String) dbOut.get("data");

// Ressurect IRP
ImmutableRatesProvider resurrectedRP  =  ser.jsonReader().read(rpOutOfDB,  ImmutableRatesProvider.class);

[Edited based on feedback from Stephen - see below]

#4

Great that you found the solution, I agree that this is the approach we recommend. You can use XML or binary as well as JSON, up to you as to what works best.

Just to note that in the last line of code, you don’t need ser.COMPACT.jsonReader(), as ser is already equal to JodaBean.Ser.COMPACT.