Giter Club home page Giter Club logo

Comments (6)

JakeWharton avatar JakeWharton commented on August 23, 2024

You can do this yourself!

public final class NonNullJsonAdapterFactory implements JsonAdapter.Factory {
  @Override
  public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
    for (Annotation annotation : annotations) {
      if ("NonNull".equals(annotation.getClass().getSimpleName())) { // Or NonNull.class.equals(..)
        JsonAdapter<Object> delegate = moshi.nextAdapter(this, type, annotations);
        return new NonNullJsonAdapter<>(delegate);
      }
    }
    return null;
  }

  static class NonNullJsonAdapter<T> extends JsonAdapter<T> {
    private final JsonAdapter<T> delagate;

    NonNullJsonAdapter(JsonAdapter<T> delagate) {
      this.delagate = delagate;
    }

    @Override public T fromJson(JsonReader reader) throws IOException {
      T value = delagate.fromJson(reader);
      if (value == null) {
        throw new IllegalStateException("value was null!");
      }
      return value;
    }

    @Override public void toJson(JsonWriter writer, T value) throws IOException {
      if (value == null) {
        throw new IllegalStateException("value was null!");
      }
      delagate.toJson(writer, value);
    }
  }
}

Putting validation inside Moshi is a dangerous precedent since it has no end to the types of validation people want.

from moshi.

Turbo87 avatar Turbo87 commented on August 23, 2024

from moshi.

JakeWharton avatar JakeWharton commented on August 23, 2024

Yep! That'd make for a good one. I don't think you're the first to ask
about it.

On Thu, Sep 10, 2015 at 4:36 PM Tobias Bieniek [email protected]
wrote:

Ah cool, I didn't know that was possible. I guess that's one more reason
for a recipe/samples project or webpage.


Reply to this email directly or view it on GitHub
#77 (comment).

from moshi.

Turbo87 avatar Turbo87 commented on August 23, 2024

@JakeWharton I just tried your code but it didn't seem to work as I had expected. What I tried was roughly the following:

public static class TestModel {
    @NonNull String a;
    String b;
}

Moshi moshi = new Moshi.Builder()
        .add(new NonNullJsonAdapterFactory())
        .build();

JsonAdapter<TestModel> adapter = moshi.adapter(TestModel.class);

// should work
assertThat(adapter.fromJson("{\"a\":\"test\"}").a).isEqualTo("test");

// should fail
adapter.fromJson("{}");

I was expecting that the second fromJson() call would throw the IllegalStateException, but it basically worked just like before. From my limited debugging it seems like the NonNullJsonAdapterFactory#create() method gets the annotations of the TestModel class itself (annotations parameter is empty), but not of its members, so annotating the members doesn't seem to have any effect.

Am I missing something?

from moshi.

swankjesse avatar swankjesse commented on August 23, 2024

I think it's the difference between rejecting null values and requiring non-null values. The example doesn't do anything about absent keys!

from moshi.

Turbo87 avatar Turbo87 commented on August 23, 2024
adapter.fromJson("{\"a\":null}");

the above call is not throwing anything either...

but you are right, what I actually want to test is whether the API response included the field and that it is non-null.

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.