-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow JSON to be passed as a String to SchemaMapper (add sourceType support in #221) #255
Comments
How I can configure the convertion like the website? |
Could you give this a try: final GenerationConfig config = new DefaultGenerationConfig() {
@Override
public SourceType getSourceType() {
return SourceType.JSON;
}
};
final JCodeModel codeModel = new JCodeModel();
final RuleFactory ruleFactory = new RuleFactory(config, new Jackson2Annotator(), new SchemaStore());
final SchemaMapper schemaMapper = new SchemaMapper(ruleFactory, new SchemaGenerator());
schemaMapper.generate(codeModel, className, packageName, "{\"titulo\": \"JSON\"}");
codeModel.build(new File("output")); |
Ah, actually it looks like the change made in #221 to allow source documents to be generated from strings doesn't use the config correctly, so it doesn't support sourceType=JSON. You'll need to use a URL to pass the JSON content. If you can't use a URL, you could also take a look inside SchemaMapper and copy some of the logic in there. It's all about calling the rulefactory with the right JsonNode. |
Hello, Using the URL the convertion works, but with string not. |
Hi, I am trying to use your utility in my project and tried in the exact way mentioned in the code snippet that you have provided. But when I do codeModel.build(new File("output")); I m getting non-existent directory exception. Then I tried to give an existing directory in it. But I don't see the generated file. Can you please help me with this issue? public void getPOJO(String JSONsource){
Thanks, |
What is the content of sample.json? Is it plain JSON or JSON schema? If it's plain json, you need to set sourceType JSON, like this: final GenerationConfig config = new DefaultGenerationConfig() {
@Override
public SourceType getSourceType() {
return SourceType.JSON;
}
};
final JCodeModel codeModel = new JCodeModel();
final RuleFactory ruleFactory = new RuleFactory(config, new Jackson2Annotator(), new SchemaStore());
final SchemaMapper schemaMapper = new SchemaMapper(ruleFactory, new SchemaGenerator()); |
I don't see any errors or exceptions but for some reason it does not create my class. Here is my code after adding your suggestion.
Thanks, |
The reason this bug is currently still open is that #221 added support for passing strings to the SchemaMapper as content, but it doesn't currently support JSON (only JSON Schema). If you want to generate JSON, you need to pass the URL to the SchemaMapper instead: schemaMapper.generate(codeModel, "MailMergeBean", "net.emailservice.new", source); |
Hello, |
@sanalsan happy to accept a pull request for this if you are interested in fixing it. |
Hello Joe, can you please tell me the error in it ? package com.schoolspeak.modelgenerator; import android.os.Bundle; import com.sun.codemodel.JCodeModel; import org.jsonschema2pojo.DefaultGenerationConfig; import java.io.File; public class MainActivity extends AppCompatActivity {
File(Environment.getExternalStorageDirectory(), "Model");
new Jackson2Annotator(),
} On Fri, Nov 27, 2015 at 4:15 PM, Joe Littlejohn notifications@github.com
Thanks & Regards, |
The error is exactly as described already:
|
Closed by #475 |
Hello,
In http://www.jsonschema2pojo.org/ I could convert JSON to POJO, but using the API this not work, only Json Schema to POJO. Is possible do this with the API?
In this case below, I didn't get exception but It doesn't generate the java file.
JCodeModel codeModel = new JCodeModel();
SchemaMapper schemaMapper = new SchemaMapper();
schemaMapper.generate(codeModel, className, packageName, "{"titulo": "JSON"}");
codeModel.build(new File("output"));
The text was updated successfully, but these errors were encountered: