npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@guimini/apex-json-serialization

v1.0.5

Published

A better alternative to `JSON.deserialize()` and `JSON.deserializeUntypped()`

Downloads

11

Readme

@guimini/apex-json-serialization

This repo offers a better alternative to JSON.deserialize() and JSON.deserializeUntypped()

Usage

  /**
   * Implement the JSONDeserializer.Deserializable on your type
   */
  public class DeserializableClass implements JSONDeserializer.Deserializable {
    private String firstname;
    public void setDeserializedField(String key, Object value) {
      switch on key {
        when 'first_name' {
          this.firstname = (String) value;
        }
        when else {
          throw new IllegalArgumentException(
            'Key [' + key + '] is not supported'
          );
        }
      }
    }
    public Map<String, System.Type> getDeserializableFields() {
      return new Map<String, System.Type>{ 'first_name' => String.class };
    }
  }

  /** call the deserializer */
  DeserializableClass simple = (DeserializableClass) JSONDeserializer.deserialize(
    '{"first_name":"John"}',
    DeserializableClass.class
  );

Deploy right away

But have a look at the code first. Don't trust random code from the internet ;)

Thanks @andyinthecloud

Install as a dependency

Alternatively, you may use this as an npm dependency

From github or npmjs

In your sfdx project folder

npm i @guimini/apex-json-serialization
OR
npm install https://github.com/guimini/apex-json-serialization

Mount it as a PackageDirectory

In your sfdx project folder

ln -s node_modules/@guimini/apex-json-serialization/force-app apex-json-serialization

Then edit your sfdx-project.json to add

 "packageDirectories": [
    ...,
    { "path": "apex-json-serialization" }
  ],

or Mount it as a source directory

In your sfdx project folder, create the folder were you want to mount the sources (for instance : force-app/dependencies)

cd path/to/folder
ln -s ../<until you are back ar your sfdx project dir>/node_modules/@guimini/apex-json-serialization/force-app/main apex-json-serialization

This will make sources accessible in force-app/dependencies/apex-json-serialization

Key Features

  • Deserializing arbitrary keys to specific members of your Deserializable class : bringing a more tedious yet functionally equivalent to GSON @SerializedName("name") https://www.javadoc.io/doc/com.google.code.gson/gson/2.6.2/com/google/gson/annotations/SerializedName.html

  • Polymorphic Deserialization : Should you need to deserialize values that may need to be deserialized to a different class depending on the content of serialized object, you may provide a Discriminator with your Polymorph Deserializable class

  • Support for Object typped members. Avoiding System.JSONException: Apex Type unsupported in JSON: Object, for there are times we simply do not know what to expect !

What's next

  • [ ] Json Serialization to arbitrary keys
  • [ ] Managed Package

Known Gotchas

  • Deserialzing to Blob type may give surprising results. I was not able to handle that properly. Please avoid it. PR welcome :)
  • If a member of class is Deserializable, then that class should implement Deserializable.
  • Performance. I didn't run any perf test, but i wouldn't be surprised if it were dramatically slower than the standard counterpart. If you look big and complex json payloads, this may be an issue.

Publish to NPM

I do not have time to setup a full CI/CD for something that should not move that much Simply run this by hand npm publish