question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trouble using conversion through JSON

See original GitHub issue

Hi,

I tried following what you said in the wiki as we have our own implementation of the OpenRTB models:

Note: If adopting openrtb-core’s RTB model classes is difficult for some reason, for example you have legacy code that uses another OpenRTB library, you can just add a step 4 that converts our OpenRtb.BidRequest to a JSON string that can be parsed back to the preferred OpenRTB model. See openrtb-core’s documentation about JSON serialization. For the response just do the reverse, creating the JSON output and parsing that to our OpenRtb.BidResponse that can be mapped to DoubleClick. Notice that these additional JSON serialization steps are very fast (in the order of ~10 microseconds), so don’t worry about extra cost.

My problem is that when I json encode the OpenRtb.BidRequest (using google GSON) most of the fields are suffixed with an underscore. For instance i have id_, imp_, imp_[0].banner_.btype_.

Our model respects the OpenRTB names (i.e. we have fields named id, imp, imp[0].banner.btype) Is there anyway to correct this besides doing a complete mapping from one object to the other ?

Thanks

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
opinalicommented, Sep 16, 2014

I see that you’re trying to use GSON (or any automatic JSON mapping API like Jackson’s ObjectMapper) to serialize the content of this library’s types like OpenRtb.BidRequest. This will not work well because protoc-generated classes are not proper POJOs; in addition to fields terminated by ‘_’ they have non-JavaBean-standard method names, they have other fields for things like absent/present bits, the UnknownFieldSet, extensions… so you are likely to have other problems in the generated JSON unless you write more workarounds. I just don’t understand why are you trying to do this serialization with GSON, when you could use the included OpenRtbJson* API?

The booleans converted to YES / NO are easy to explain: first, the OpenRTB specification doesn’t really specify any API, only a JSON format which we do implement strictly (the generated JSON validates correctly with Nexage’s OpenRTB Validator, but let me know if you see any exception). The concrete API that we define with protobuf fills in some gaps or fixes some issues, like the use of 0/1 values for many boolean flags without any good reason (other than the fact that 0/1 use less space in JSON text…). But the OpenRtbJson* API will correctly serialize these flags as 0/1.

0reactions
jihonradocommented, Jun 3, 2015

Hi,

Here is the working implementation for the latest version (0.8.4).

Mapper:

import com.fasterxml.jackson.core.JsonParser;
import com.google.doubleclick.DcExt;
import com.google.openrtb.json.OpenRtbJsonExtReader;
import com.google.openrtb.json.OpenRtbJsonUtils;
import com.google.protobuf.GeneratedMessage;
import com.google.protos.adx.NetworkBid;

import java.io.IOException;

public class ClickThroughUrlMapper<EB extends GeneratedMessage.ExtendableBuilder<?, EB>> extends OpenRtbJsonExtReader<EB, NetworkBid.BidResponse.Ad.Builder> {

    public ClickThroughUrlMapper() {
        super(DcExt.ad);
    }

    @Override
    protected void read(EB msg, NetworkBid.BidResponse.Ad.Builder ad, JsonParser par) throws IOException {
        switch (OpenRtbJsonUtils.getCurrentName(par)) {
            case "click_through_url":
                for (OpenRtbJsonUtils.startArray(par); OpenRtbJsonUtils.endArray(par); par.nextToken()) {
                    ad.addClickThroughUrl(par.getText());
                }
        }
    }

}

Registration:

OpenRtbJsonFactory  jsonFactory = OpenRtbJsonFactory.create().register(new ClickThroughUrlMapper(), OpenRtb.BidResponse.SeatBid.Bid.Builder.class);

The rest is the same @Crystark exposed.

Best,

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conversion Casting Issues - json - Stack Overflow
I am developing a small side scroller game using slick 2D lwjgl and am running into a current error while casting something. It...
Read more >
Issues with Parse JSON after converting from XML
I can successfully run a GET call for the API, convert to XML (base64toString conversion), compose the XML, and then compose the JSON....
Read more >
Error while converting JSON object to TW object - Forums - IBM
I am trying to convert a JSON object to TW object using below mentioned code. //var jsonObject = JSON.parse(tw.local.data);. //if(jsonObject.
Read more >
[ardoJSON] Having trouble converting JSON to Record List
ardoJSON. Forge component by João Barata. Hi Ricardo, I'm using JSON2RecordList and I am not able to get it to convert. The record...
Read more >
Converting JSON into Javascript Objects with JSON.parse()
If you force code into a JSON object with a string, you must use the Javascript eval() function to convert it into something...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found