json2apex icon indicating copy to clipboard operation
json2apex copied to clipboard

Constructor not defined: [Object].<Constructor>(System.JSONParser)

Open textual opened this issue 7 years ago • 4 comments

I'm trying to use this for Shopify Orders and a few things are falling into this Object category which results in not being able to save since an object constructor doesn't seem to be valid. Any advice on working around these issues?

textual avatar Aug 17 '17 22:08 textual

Do you have more info, a sample json that causes an issue?

superfell avatar Aug 17 '17 23:08 superfell

ShopifyOrder_Error (2).zip resulting zip file

I used this tool a few months back for what is currently running and had to make quite a few mods to get it to save correctly. It works for about 70% of the data were trying to pull back but now that I'm trying to increase that success rate, I'd like to understand what's going on with some of the anomalies that needed to be addressed for original deployment. Attached is large json of orders from Shopify.

RT_Shopify_Sample.json.zip

textual avatar Aug 17 '17 23:08 textual

The issue mentioned in the title here is fixed, however there are other issues with the shopify sample that aren't fixed. [seem to be hitting some kind of expr size limit].

superfell avatar Mar 10 '18 22:03 superfell

I have the same or similar issue. Constructor not defined: [BookingsWrapper.Errors].<Constructor>(JsonParser) Here is a sample - largely reduced - json2apex outcome

`// // Generated by JSON2Apex http://json2apex.herokuapp.com/ // // The supplied json has fields with names that are not valid in apex // and so can only be parsed with explicitly generated code, this option // was auto selected for you.

public class BookingsWrapper {

public class Miscellaneous {
	public Integer id {get;set;} 
	public List<Place> place {get;set;} 
	public String resort {get;set;} 
	public String name {get;set;} 
	public String information {get;set;} 
	public List<CreditCards> customFields {get;set;} 

	public Miscellaneous(JSONParser parser) {
		while (parser.nextToken() != System.JSONToken.END_OBJECT) {
			if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
				String text = parser.getText();
				if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
					if (text == 'id') {
						id = parser.getIntegerValue();
					} else if (text == 'place') {
						place = arrayOfPlace(parser);
					} else if (text == 'resort') {
						resort = parser.getText();
					} else if (text == 'name') {
						name = parser.getText();
					} else if (text == 'information') {
						information = parser.getText();
					} else if (text == 'customFields') {
						customFields = arrayOfCreditCards(parser);
					} else {
						System.debug(LoggingLevel.WARN, 'Miscellaneous consuming unrecognized property: '+text);
						consumeObject(parser);
					}
				}
			}
		}
	}
}

[...]

public BookingsWrapper(JSONParser parser) {
	while (parser.nextToken() != System.JSONToken.END_OBJECT) {
		if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
			String text = parser.getText();
			if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
				if (text == 'channel') {
					channel = parser.getIntegerValue();
				} else if (text == 'language') {
					language = parser.getText();
				} else if (text == 'timeStamp') {
					timeStamp = parser.getText();
				} else if (text == 'success') {
					success = parser.getBooleanValue();
				} else if (text == 'bookings') {
					bookings = arrayOfBookings(parser);
				} else {
					System.debug(LoggingLevel.WARN, 'BookingsWrapper consuming unrecognized property: '+text);
					consumeObject(parser);
				}
			}
		}
	}
}	

public static BookingsWrapper parse(String json) {
	System.JSONParser parser = System.JSON.createParser(json);
	return new BookingsWrapper(parser);
}


private static List<CreditCards> arrayOfCreditCards(System.JSONParser p) {
    List<CreditCards> res = new List<CreditCards>();
    if (p.getCurrentToken() == null) p.nextToken();
    while (p.nextToken() != System.JSONToken.END_ARRAY) {
        res.add(new CreditCards(p));
    }
    return res;
}

}`

While I was trying to solve it I also came across the following exception: Illegal assignment from System.JSONParser to JsonParser

So to solve the issue I added System. to every sub class constructor, ie: public Miscellaneous(System.JSONParser parser) {} public BookingsWrapper(System.JSONParser parser) {}

That worked form me.

tpiechota avatar Feb 10 '20 10:02 tpiechota