logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <garydgreg...@gmail.com>
Subject Re: LOG4J2-583, XML and JSON unmarshalling
Date Tue, 01 Apr 2014 21:48:57 GMT
Also interesting: Google GSON in on my Eclipse project CP, so it must be
brought in... somehow...

Gary


On Tue, Apr 1, 2014 at 5:37 PM, Gary Gregory <garydgregory@gmail.com> wrote:

> JacksonXmlProperty? Why not JsonProperty? Couldn't you then use JSON or
> XML? Still learning Jackson...
>
> Gary
>
>
> On Tue, Apr 1, 2014 at 4:49 PM, Ralph Goers <ralph.goers@dslextreme.com>wrote:
>
>> I just did this the other day for both XML and JSON.
>>
>> For XML I did:
>>
>>     public static TransactionRequest deserialize(String xmlFile) {
>>         try {
>>             XmlMapper mapper = new XmlMapper();
>>             InputStream stream = new FileInputStream(xmlFile);
>>             return mapper.readValue(stream, TransactionRequest.class);
>>         } catch (Exception ex) {
>>             ex.printStackTrace();
>>         }
>>         return null;
>>     }
>>     public static TransactionRequest deserialize(String xmlFile) {
>>         try {
>>             XmlMapper mapper = new XmlMapper();
>>             InputStream stream = new FileInputStream(xmlFile);
>>             return mapper.readValue(stream, TransactionRequest.class);
>>         } catch (Exception ex) {
>>             ex.printStackTrace();
>>         }
>>         return null;
>>     }
>>
>> Where needed I used things like:
>>
>>     @JacksonXmlProperty(localName = "return")
>>     private PostSale creditCardReturn;
>>
>> and
>>
>>     @JacksonXmlProperty(isAttribute = true)
>>
>> For JSON I did
>>
>>     public static JSONRequest deserialize(String json)
>>     {
>>         try
>>         {
>>             ObjectMapper mapper = new ObjectMapper();
>>             return mapper.readValue(json, JSONRequest.class);
>>         }
>>         catch (Exception ex)
>>         {
>>             ex.printStackTrace();
>>         }
>>         return null;
>>     }
>>
>> and
>>
>>     public String serialize()
>>     {
>>         try
>>         {
>>             final ObjectMapper mapper = new ObjectMapper();
>>             return mapper.writeValueAsString(this);
>>         }
>>         catch (Exception ex)
>>         {
>>             ex.printStackTrace();
>>         }
>>         return null;
>>     }
>>
>> HTH,
>>
>> Ralph
>>
>>
>> On Apr 1, 2014, at 1:05 PM, Gary Gregory <garydgregory@gmail.com> wrote:
>>
>> Well... so much for Jackson making my life easy. Jackson says it
>> "supports" JAXB annotations but that must be only for the simplest cases.
>> Jackson does not work with the JAXB annotations I used on Log4jLogEvents.
>> This suppose this is not surprising. Back to the drawing board...
>>
>> Gary
>>
>>
>> On Tue, Apr 1, 2014 at 12:05 PM, Gary Gregory <garydgregory@gmail.com>wrote:
>>
>>> All good ideas, thank you. The JSON API I know best is GSON, which let's
>>> you listen to objects opening and closing. Maybe Jackson has something like
>>> that... I'll have to dig in.
>>>
>>> Gary
>>>
>>>
>>> On Tue, Apr 1, 2014 at 11:14 AM, Ralph Goers <ralph.goers@dslextreme.com
>>> > wrote:
>>>
>>>> So you are hacking the stream before passing it to the unmarshalling
>>>> framework?  Then you will have to keep track of the ‘{‘ and ‘}’ characters
>>>> yourself, either with the stack Matt suggests or as a counter.
>>>>
>>>> Ralph
>>>>
>>>> On Apr 1, 2014, at 8:07 AM, Matt Sicker <boards@gmail.com> wrote:
>>>>
>>>> Keep a stack of {'s and pop them when you get a }. Like a deterministic
>>>> pushdown automaton.
>>>>
>>>>
>>>> On 1 April 2014 07:45, Gary Gregory <garydgregory@gmail.com> wrote:
>>>>
>>>>> I have a local patch for LOG4J2-583 to have the Log4j TCP and UDP
>>>>> socket servers unmarhsal XML log events.
>>>>>
>>>>> This is "easy" for XML because when you have a stream of bytes and you
>>>>> know its encoding, you can look for the end of an event by looking for
its
>>>>> closing tag: </Event>. Right now, my XML processing code, looks
for the end
>>>>> tag and feeds JAXB a substring from the buffer. Easy. Done.
>>>>>
>>>>> Not so much with JSON. You cannot use the same hack, there is no end
>>>>> tag. All you have is an "end of object" closing bracket "}" which looks
the
>>>>> same as the closing marker for all other objects.
>>>>>
>>>>> So it looks like I would need to hook in a little deeper into a JSON
>>>>> unmarshalling framework to extract each JSON log events as I see them.
>>>>>
>>>>> Any thoughts here?
>>>>>
>>>>> Gary
>>>>>
>>>>> --
>>>>> E-Mail: garydgregory@gmail.com | ggregory@apache.org
>>>>> Java Persistence with Hibernate, Second Edition<http://www.manning.com/bauer3/>
>>>>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>>>>> Spring Batch in Action <http://www.manning.com/templier/>
>>>>> Blog: http://garygregory.wordpress.com
>>>>> Home: http://garygregory.com/
>>>>> Tweet! http://twitter.com/GaryGregory
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Matt Sicker <boards@gmail.com>
>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> E-Mail: garydgregory@gmail.com | ggregory@apache.org
>>> Java Persistence with Hibernate, Second Edition<http://www.manning.com/bauer3/>
>>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>>> Spring Batch in Action <http://www.manning.com/templier/>
>>> Blog: http://garygregory.wordpress.com
>>> Home: http://garygregory.com/
>>> Tweet! http://twitter.com/GaryGregory
>>>
>>
>>
>>
>> --
>> E-Mail: garydgregory@gmail.com | ggregory@apache.org
>> Java Persistence with Hibernate, Second Edition<http://www.manning.com/bauer3/>
>> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
>> Spring Batch in Action <http://www.manning.com/templier/>
>> Blog: http://garygregory.wordpress.com
>> Home: http://garygregory.com/
>> Tweet! http://twitter.com/GaryGregory
>>
>>
>>
>
>
> --
> E-Mail: garydgregory@gmail.com | ggregory@apache.org
> Java Persistence with Hibernate, Second Edition<http://www.manning.com/bauer3/>
> JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
> Spring Batch in Action <http://www.manning.com/templier/>
> Blog: http://garygregory.wordpress.com
> Home: http://garygregory.com/
> Tweet! http://twitter.com/GaryGregory
>



-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory

Mime
View raw message