Do you know any java library (or sth other maybe script?) that allows you to 'convert' LDIF to JSON?
JSON data is easier for me to process, So an easy way to make such conversion would be really useful. Also I don't want to reinvent the wheel:)
Cheers
This seems to be what you are after.
You should be able to do something like:
StringWriter writer = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(writer);
FileReader reader = new FileReader("entry.ldif");
LdifReader ldifReader = new LdifReader(reader);
SearchResponse result = ldifReader.read();
jsonWriter.write(result);
System.out.println(writer.toString());
Here is a PHP script I found on github as well that does from ldif to JSON or XML.
I don't know the quality of it.
Related
Is it possible to somehow write a BsonDocument object into file like BSON (not in JSON format)? I am using Java with MongoDB Java Driver for writing BsonDocuments.
I am trying somthing like this:
BsonDocument bson = BsonDocument.parse(someJSONString);
bson.writeBSONtoFile("someFilepath"); //this method
I know that this method will not work, but I am looking for something like this.
OK, I have found the workaround.
The json string convert to bsonDocument and then with DocumentCodec get byte array which it is written into file.
BsonDocument bson = BsonDocument.parse(someJSONString);
BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
new BsonDocumentCodec().encode(writer, bson, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
byte[] byteArr = outputBuffer.toByteArray();
writeToFile(byteArr);
Many thanks to this guy/post
How to directly convert MongoDB Document do Jackson JsonNode in Java
I want to save the entities in our program into .json files to get a better connection between backend and our Angular frontend. For this, I wrote some tests and during the execution, the structure is saved in the files as desired.
The structure is sampled by
ObjectMapper objectMapper = new ObjectMapper();
try{
ObjectWriter writer = objectMapper.wrtier(new DefaultPrettyPrinter());
String result = objectMapper.writerWirthDefaultPrettyPrinter().writeValueAsString(new OurObject());
writer.writerValue(new File("path"), result);
}
What I got
"{\r\n \"firstProp\": something,\r\n \"secondProp\": anything,\r\n...
But I want, that the file contains the classical JSON structure to make it better readable, this means:
{
"firstProp": something,
"secondProp": anything,
...
What can I do, to write it in the desired JSON structure?
Thanks for any help
Matthias
You're double-encoding the json string
Remove writeValueAsString and try to directly use writer.writerValue(file, object)
But if you're emitting this from a Java backend, it's typically best practice to serve it from an HTTP request, not as a file to any front-end
I want to format the output XML generated by Xstream, to make it more readable. Currently, a newline is added after each element but I would like newline to be added after every attribute. Is there a way to do this?
Pretty Print Writer is used by default to format the output of the xml but this doesn't suffice for me. I want newline to be added after every
XStream includes a PrettyPrintWriter
After building your XStream...
XStream xstream = //...whatever
Instead of:
// p is my object needing xml serialization
xstream.toXML(p)
Use something like this to make it pretty:
BufferedOutputStream stdout = new BufferedOutputStream(System.out);
xstream.marshal(p, new PrettyPrintWriter(new OutputStreamWriter(stdout)));
Take a look at their tutorial on tweaking the output.
I've used this to :
xstream= new XStream(new DomDriver());
But it's not so efficient than StaxDriver()
I know Tika has a very nice wrapper that let's me get a Reader back from parsing a file like so:
Reader parsedReader = tika.parse(in);
However, if I use this, I cannot specify the parser that I want and the metadata that I want to pass in. For example, I would want to pass in extra info like which handler, parser, and context to use, but I can't do it if I use this method. As far as I know, it's the only one that let's me get a Reader instance back and read incrementally instead of getting the entire parsed string back.
Example of things I want to include:
Parser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler(-1);
Metadata metadata = new Metadata();
metadata.set(Metadata.RESOURCE_NAME_KEY, fileName); //This aids in the content detection
ParseContext context = new ParseContext();
context.set(Parser.class, parser);
parser.parse(is, handler, metadata, context);
However, calling parse on a parser directly does not return a reader, and the only option I have(noticed in the docs) is to return a fully parsed string, which might not be great for memory usage. I know I can limit the string that is returned, but I want to stay away from that as I wanto the fully parsed info, but in incremental fashion. Best of both world, is this possible?
One of the many great things about Apache Tika is that it's open source, so you can see how it works. The class for the Tika facade you're using is here
The key bit of that class for your interest is this bit:
public Reader parse(InputStream stream, Metadata metadata)
throws IOException {
ParseContext context = new ParseContext();
context.set(Parser.class, parser);
return new ParsingReader(parser, stream, metadata, context);
}
You see there how Tika is taking a parser and a stream, and processing it to a Reader. Do something similar and you're set. Alternately, write your own ContentHandler and call that directly for full control!
I am using json developing android sdk.
I found writing json string is annoying, take a look:
post_my_server("{\"cmd\":\"new_comment\"}");
I need to manually escape quotes, is there any clean way to do this?
You can use single quotes "{'cmd':'new_comment'}".
Alternatively, there is free code at http://json.org/java/ for implementing JSON at an object level. It's free as in very permissive, I am no lawyer, but it would seem that the only stipulation is that the software you include it in is good, and not evil.
To create json string use jackson core jars. The code snippet to generate a sample json string ,
JsonFactory jFactory = new JsonFactory();
StringWriter writer = new StringWriter();
/*** write to string ***/
JsonGenerator jsonGenerator = jFactory.createJsonGenerator(writer);
jsonGenerator.writeStartObject();
jsonGenerator.writeStringField("company", [Value]);
jsonGenerator.writeNumberField("salary", [value]);
jsonGenerator.writeEndObject();
jsonGenerator.close();
String jsonString = writer.toString();
I use the jackson json parsers and serializers , they are completely self contained, and allow for reading, writing of any java object to and from json.
Assume you have a file called "user.json" which contains a big json in it....
private static void convert(){
Map<String,Object> userData = mapper.readValue(new File("user.json"), Map.class);
}