GithubHelp home page GithubHelp logo

workingoftimtohyzhang / dummymapper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from goodforgod/dummymapper

0.0 1.0 0.0 383 KB

IntelliJ IDEA plugin for mapping Java class to JSON/AVRO/GraphQL.

Home Page: https://plugins.jetbrains.com/plugin/14911

License: Apache License 2.0

Java 100.00%

dummymapper's Introduction

DummyMapper ๐Ÿ—Ž

Java CI Quality Gate Status Maintainability Rating Reliability Rating

Intellij IDEA plugin for mapping Java Classes to formats like JSON/AVRO/GraphQL/etc.

Content

Installation

  • Install using Intellij Plugin marketplace:
    • Navigate to site and click install button there.
  • Using IDE built-in plugin system on:
    • [Windows] File > Settings > Plugins > Marketplace > Search for "DummyMapper" > Install
    • [MacOS] Preferences > Settings > Plugins > Marketplace > Search for "DummyMapper" > Install
  • Manually:
    • Download the latest release, compile with ./gradlew buildPlugin and install it manually using File > Settings > Plugins > โš™๏ธ > Install Plugin from Disk...

Format Support

Plugin allow mapping Java Classes to different formats, this section describes information about supported formats and their options.

All format examples will be showed according to this class as example. Keep in mind that mapping is based on class fields, not getters\setters.

public class User {

    private class Credential {
        private String id;
        private long issued;
    }

    private UUID id;
    private String name;
    private List<String> roles;
    private Credential credential;
}

Json

Plugin uses DummyMaker library for producing Java Classes and then converting them in JSON format.

Allow mapping Java class as JSON example with fields filled with random values as if class example was serialized to JSON format.

  • Mapping options.. > Map as JSON
{
  "id" : "76fbb591-8e95-4df7-94ad-5d0606709141",
  "name" : "Alan",
  "roles" : [ "herbalist", "recycling-officer", "exploration-geologist" ],
  "credential" : {
    "id" : "a7ec6315-4292-454c-ac69-e61a7a36e07d",
    "issued" : 1565743683
  }
}

Also allow mapping class as array of JSONs.

  • Mapping options.. > Map as JSON Array

You can specify number of entries to generate in array.

[
  {
    "id": "526686d0-8d84-4b85-a751-77fa7a157a69",
    "name": "Rachel",
    "roles": [ "fitness-centre-manager", "plant-breeder", "television-production-assistant" ],
    "credential": {
      "id": "94f349c9-405a-4921-8dbf-8abfacc28cf1",
      "issued": 1548018277
    }
  }
]

Annotations Support

Annotations from Jackson are supported when serializing to JSON, but keep in mind that only fields used for serialization, not getters.

However, Jackson annotations from respected fields getters will be applied during serialization if found, but such behavior is not guarantied to be always correct.

Example for User class:

public class User {
    
    private UUID id;
    @JsonProperty(value = "firstName")
    private String name;
    private String surname;
    @JsonIgnore
    private List<String> roles;

    @JsonIgnore
    public String getSurname() {
        return surname;
    }
}

Mapping User class as JSON will result in (most of Jackson annotations and their parameters are supported):

{
  "id": "975e80ed-b95d-46b2-9338-519ff7083dd3",
  "firstName": "Alfred"
}

Json Schema

Allow mapping Java class as JSON Schema, 3 different drafts are available:

Mapping is under:

  • Mapping options.. > Map as JSON Schema
{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "type": "object",
  "properties": {
    "credential": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "issued": {
          "type": "integer"
        }
      }
    },
    "id": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "roles": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  }
}

Avro Schema

Allow mapping Java class as AVRO Schema for version 1.9.2

There is option for two different AVRO Schema builders like:

Mapping is under:

  • Mapping options.. > Map as AVRO Schema
{
  "type" : "record",
  "name" : "User",
  "namespace" : "io.goodforgod.dummymapper",
  "fields" : [ {
    "name" : "id",
    "type" : {
      "type" : "record",
      "name" : "UUID",
      "namespace" : "java.util",
      "fields" : [ ]
    }
  }, {
    "name" : "name",
    "type" : "string"
  }, {
    "name" : "roles",
    "type" : {
      "type" : "array",
      "items" : "string",
      "java-class" : "java.util.List"
    }
  }, {
    "name" : "credential",
    "type" : {
      "type" : "record",
      "name" : "Credential",
      "fields" : [ {
        "name" : "id",
        "type" : "string"
      }, {
        "name" : "issued",
        "type" : "long"
      } ]
    }
  } ]
}

Jackson Annotation Support

Jackson annotations are supported when serializing to AVRO Schema, but keep in mind that only fields used for serialization, not getters.

Most of Jackson annotations and their parameters are supported.

Example on how to mark field as required (Option Required By Default apply such transformation for all fields if selected):

public class User {
    
    @JsonProperty(required = true)
    private UUID id;
    private String name;
    private String surname;
}

Apache Annotation Support

Annotations from Apache Avro library supported when serializing to AVRO Schema.

GraphQL

Allow mapping Java class as GraphQL query with version v14

Mapping is under:

  • Mapping options.. > Map as GraphQL
schema {
  query: Query
}

type Credential {
  id: String
  issued: Long!
}

#Query root
type Query {
  credential: Credential
  id: UUID
  name: String
  roles: [String]
}

#Long type
scalar Long

#Unrepresentable type
scalar UNREPRESENTABLE

#UUID String
scalar UUID

Annotation Support

Annotations from GraphQL SPQR library supported when serializing to GraphQL.

Limitations

There some limitations for plugin when mapping classes:

  • Enums are not supported and thus will be displayed as String fields, except for Map as JSON where enum values will be generated correctly. This issue is due to library used in plugin unable to create Java Enums.
  • Getters, Setters not used for any mapping, only annotations from getters are used for mapping (depends on operation) if found. Standard Java naming convention expected from getters to be found.
  • Annotations Class Params are not supported. Thus, most of annotation parameters like String, Boolean, etc. are supported, but Class or other complex parameters not supported.

Version History

1.0.0 - Initial project with support for Json, Json array, Json Schema, Avro Schema, GraphQL formats.

Licence

This project licensed under the Apache 2.0 - see the LICENSE file for details.

dummymapper's People

Contributors

goodforgod avatar

Watchers

 avatar

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.