You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by Bill van Melle <bi...@gmail.com> on 2010/11/23 00:58:58 UTC

An actual plain text serializer?

I noticed that one of the implementations of Serializer<T> is the curiously
named PlainTextSerializer, which apparently returns a Document!  I'm trying
to imagine what that would be (I couldn't find any (ahem) documentation of
Document other than the rather unhelpful javadoc), but meanwhile, what if I
really did want to read a web result as plain text?  Am I supposed to
use ByteArraySerializer and then convert it to a String?  Not particularly
hard, I suppose -- in fact, it's trivial to write my own StringSerializer
that uses ByteArraySerializer internally.  I was just wondering why I should
be the one to do that :).


public class StringSerializer implements Serializer<String> {

    private String encoding = "UTF8";

    public StringSerializer() {
    }

    public StringSerializer(String encoding) {
        this.encoding = encoding;
    }

    @Override
    public String getMIMEType(String object) {
        return "text/plain";
    }

    @Override
    public String readObject(InputStream inputStream)
            throws IOException, SerializationException {
        byte[] bytes = new ByteArraySerializer().readObject(inputStream);
        return new String(bytes, encoding);
    }

    @Override
    public void writeObject(String object, OutputStream outputStream)
            throws IOException, SerializationException {
        byte[] bytes = object.getBytes(encoding);
        new ByteArraySerializer().writeObject(bytes, outputStream);
    }

}

Re: An actual plain text serializer?

Posted by Sandro Martini <sa...@gmail.com>.
You can find the ticket here: https://issues.apache.org/jira/browse/PIVOT-672

Related sources has been committed in trunk few minutes ago, but are a
little different than proposed (they are more aligned with other Pivot
Serializers). Let me know if there is something to fix ...

Bye,
Sandro

Re: An actual plain text serializer?

Posted by Greg Brown <gk...@mac.com>.
Yep - you definitely still have time. Thanks.

On Nov 23, 2010, at 11:31 AM, Sandro Martini wrote:

> 
> Hi to all,
> I like this idea, I can write it (and related test class) this evening (and
> add the JIRA ticket), but am I still in time for the 2.0 ?
> 
> Bye,
> Sandro
> 
> -- 
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/An-actual-plain-text-serializer-tp1949891p1954463.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.


Re: An actual plain text serializer?

Posted by Sandro Martini <sa...@gmail.com>.
Hi to all,
I like this idea, I can write it (and related test class) this evening (and
add the JIRA ticket), but am I still in time for the 2.0 ?

Bye,
Sandro

-- 
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/An-actual-plain-text-serializer-tp1949891p1954463.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: An actual plain text serializer?

Posted by Greg Brown <gk...@mac.com>.
PlainTextSerializer is used to read a text file into an org.apache.pivot.wtk.text.Document, for use in a TextPane. It is intended to parallel a hypothetical HTMLSerializer, which would read basic HTML into a Document.

A StringSerializer class doesn't exist because we haven't had any use case for one yet. Feel free to submit your code as a patch if you'd like to see it included in the platform (this is an open source project, after all).  :-)

G


On Nov 22, 2010, at 6:58 PM, Bill van Melle wrote:

> I noticed that one of the implementations of Serializer<T> is the curiously named PlainTextSerializer, which apparently returns a Document!  I'm trying to imagine what that would be (I couldn't find any (ahem) documentation of Document other than the rather unhelpful javadoc), but meanwhile, what if I really did want to read a web result as plain text?  Am I supposed to use ByteArraySerializer and then convert it to a String?  Not particularly hard, I suppose -- in fact, it's trivial to write my own StringSerializer that uses ByteArraySerializer internally.  I was just wondering why I should be the one to do that :).
> 
> 
> public class StringSerializer implements Serializer<String> {
>     
>     private String encoding = "UTF8";   
> 
>     public StringSerializer() {
>     }
> 
>     public StringSerializer(String encoding) {
>         this.encoding = encoding;
>     }
> 
>     @Override
>     public String getMIMEType(String object) {
>         return "text/plain";
>     }
> 
>     @Override
>     public String readObject(InputStream inputStream) 
>             throws IOException, SerializationException {
>         byte[] bytes = new ByteArraySerializer().readObject(inputStream);
>         return new String(bytes, encoding);
>     }
> 
>     @Override
>     public void writeObject(String object, OutputStream outputStream)
>             throws IOException, SerializationException {
>         byte[] bytes = object.getBytes(encoding);
>         new ByteArraySerializer().writeObject(bytes, outputStream);
>     }
> 
> }