package org.realityforge.replicant.server.ee;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.StringReader;
import javax.annotation.Nonnull;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonReaderFactory;

public final class JsonUtil
{
  private static final ObjectMapper c_jsonMapper = new ObjectMapper();
  private static final JsonReaderFactory c_readerFactory = Json.createReaderFactory( null );

  private JsonUtil()
  {
  }

  @Nonnull
  public static JsonObject toJsonObject( @Nonnull final String content )
  {
    final StringReader stringReader = new StringReader( content );
    try ( final JsonReader reader = c_readerFactory.createReader( stringReader ) )
    {
      return reader.readObject();
    }
  }

  @Nonnull
  public static JsonObject toJsonObject( @Nonnull final Object object )
  {
    try
    {
      return toJsonObject( c_jsonMapper.writeValueAsString( object ) );
    }
    catch ( final IOException ioe )
    {
      throw new IllegalStateException( ioe.getMessage(), ioe );
    }
  }
}

@@ -182,16 +182,50 @@
 onnull;%0A
+import javax.annotation.Nullable;%0A
 import j
@@ -943,16 +943,156 @@
   %7B%0A    
+return toJsonObject( toJsonString( object ) );%0A  %7D%0A%0A  @Nonnull%0A  public static String toJsonString( @Nullable final Object object )%0A  %7B%0A    
 try%0A    
@@ -1105,30 +1105,16 @@
   return
- toJsonObject(
  c_jsonM
@@ -1141,26 +1141,24 @@
 ring( object
- )
  );%0A    %7D%0A  

Extract a helper method