Giter Club home page Giter Club logo

Comments (11)

inder123 avatar inder123 commented on August 23, 2024

Shouldn't the returned object be String?

from moshi.

swankjesse avatar swankjesse commented on August 23, 2024

This returns an object that can be converted to JSON, not a JSON object itself. Example:

class Point {
  double x;
  double y;

  public Object toJson() {
    return Arrays.asList(x, y);
  }
  public static Object fromJson(Object json) {
    List list = (List) json;
    return new Point((Double) list.get(0), (Double) list.get(1));
  }
}

from moshi.

inder123 avatar inder123 commented on August 23, 2024

and what is the advantage of that over returning a String?

from moshi.

swankjesse avatar swankjesse commented on August 23, 2024

The advantage is in how the data looks in the finished JSON document. Imagine we have this:

  {
    "path": [[1,2],[3,4],[3,2],[1,4]]
  }

You could marshall this with this code, without registering any type adapters:

  class Shape {
    List<Point> path;
  }

from moshi.

iNoles avatar iNoles commented on August 23, 2024

even

class Shape {
    Point[] path;
}

?

from moshi.

swankjesse avatar swankjesse commented on August 23, 2024

Yup; that too.

from moshi.

inder123 avatar inder123 commented on August 23, 2024

I am dense, need more explanation. How is the same not achieved if toJson() returned a String instead.

from moshi.

inder123 avatar inder123 commented on August 23, 2024

toJson() is meant to customize JSON output to be different from the default representation based on the fields. I presume you meant that Shape class would not need to define toJson() because List and Point[] will automatically get serialized correctly. Well, that is no different from the default behavior based on fields.

from moshi.

pforhan avatar pforhan commented on August 23, 2024

Maybe the names could be a bit clearer? I don't have a good recommendation, though.

from moshi.

swankjesse avatar swankjesse commented on August 23, 2024

It’s in, and the final version looks a lot like @Provider methods.

  static class PointAsListOfIntegersJsonAdapter {
    @ToJson List<Integer> pointToJson(Point point) {
      return Arrays.asList(point.x, point.y);
    }

    @FromJson Point pointFromJson(List<Integer> o) throws Exception {
      if (o.size() != 2) throw new Exception("Expected 2 elements but was " + o);
      return new Point(o.get(0), o.get(1));
    }
  }

from moshi.

johnjohndoe avatar johnjohndoe commented on August 23, 2024

@swankjesse Thanks for sharing the adapter snippet!

from moshi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.