Theme
JsonConverter
JsonConverter can be used for setting a value from an URL request parameter into a bean. This can be achieved with f:viewParam and attached pe:convertJson. Any primitive type, array, non generic or generic type is supported. Use RequestParameterBuilder from PrimeFaces Extensions to build any URL parameters in JSON representation and pe:convertJson to convert values back from JSON before they are set into bean. This feature allows to pre-initialize any UI forms with defaul values passed in URL on initial page load (GET request).

Primitive, plain values don't need to be converted to / from JSON. The attribute "type" in the pe:convertJson specifies data type of the value object. Data type is sometimes required to convert a complex or generic value from the JSON representation. All data types should be fully qualified. Examples:
  • "boolean"
  • "int"
  • "long[]"
  • "java.lang.String"
  • "java.util.Date"
  • "java.util.Collection<java.lang.Integer>"
  • "java.util.Map<java.lang.String, com.prime.FooPair<java.lang.Integer, java.util.Date>>"
  • "com.prime.FooNonGenericClass"
  • "com.prime.FooGenericClass<java.lang.String, java.lang.Integer>"
  • "com.prime.FooGenericClass<int[], com.prime.FooGenericClass<com.prime.FooNonGenericClass, java.lang.Boolean>>"
This example demonstrate using with primitive (like boolean and long), arrays (like int[] and char[]), simple data types (like String and Integer), date (java.util.Date), parametrized collections and maps, complex generic and non generic classes. Click on the button "Open Link" to see that values of URL parameters are set into the bean on intial page load.

Attention: URLs longer than 2083 characters may not work properly in old IE versions. Modern Firefox, Opera, and Safari can handle at least 80000 characters in URL.
booleanfalse
long0
int[]null
char[]null
String
Integer
Date
Generic List[]
Generic Map{}
Non GenericFooNonGeneric[count=0,endDate=<null>,messages=[],startDate=<null>]
Generic SimpleFooGeneric[a=<null>,b=<null>]
Generic ComplexFooGeneric[a=<null>,b=<null>]
Source

<!-- f:metadata on this page -->
<f:metadata>    
    <f:viewParam name="b" value="#{viewParamJsonController.b}"/>
    <f:viewParam name="l" value="#{viewParamJsonController.l}"/>
    <f:viewParam name="ints" value="#{viewParamJsonController.ints}">
        <pe:convertJson type="int[]"/>
    </f:viewParam>
    <f:viewParam name="chars" value="#{viewParamJsonController.chars}">
        <pe:convertJson type="char[]"/>
    </f:viewParam>
    <f:viewParam name="s" value="#{viewParamJsonController.s}">
        <pe:convertJson type="java.lang.String"/>
    </f:viewParam>
    <f:viewParam name="i" value="#{viewParamJsonController.i}"/>
    <f:viewParam name="d" value="#{viewParamJsonController.d}">
        <pe:convertJson type="java.util.Date"/>
    </f:viewParam>
    <f:viewParam name="list" value="#{viewParamJsonController.list}">
        <pe:convertJson type="#{typesJsonController.typeGenericList}"/>
    </f:viewParam>
    <f:viewParam name="map" value="#{viewParamJsonController.map}">
        <pe:convertJson type="#{typesJsonController.typeGenericMap}"/>
    </f:viewParam>
    <f:viewParam name="fooNonGeneric" value="#{viewParamJsonController.fooNonGeneric}">
        <pe:convertJson type="org.primefaces.extensions.showcase.model.jsonconverter.FooNonGeneric"/>
    </f:viewParam>
    <f:viewParam name="fooGenericSimple" value="#{viewParamJsonController.fooGenericSimple}">
        <pe:convertJson type="#{typesJsonController.typeGenericSimple}"/>
    </f:viewParam>
    <f:viewParam name="fooGenericComplex" value="#{viewParamJsonController.fooGenericComplex}">
        <pe:convertJson type="#{typesJsonController.typeGenericComplex}"/>
    </f:viewParam>
</f:metadata>
                
<h:panelGrid columns="2" columnClasses="formColumn" style="margin-bottom:15px;">
    <h:outputText value="boolean" styleClass="bold"/>
    <h:outputText value="#{viewParamJsonController.b}"/>

    <h:outputText value="long" styleClass="bold"/>
    <h:outputText value="#{viewParamJsonController.l}"/>

    <h:outputText value="int[]" styleClass="bold"/>
    <h:outputText value="#{viewParamJsonController.prettyInts}"/>

    <h:outputText value="char[]" styleClass="bold"/>
    <h:outputText value="#{viewParamJsonController.prettyChars}"/>

    <h:outputText value="String" styleClass="bold"/>
    <h:outputText value="#{viewParamJsonController.s}"/>

    <h:outputText value="Integer" styleClass="bold"/>
    <h:outputText value="#{viewParamJsonController.i}"/>

    <h:outputText value="Date" styleClass="bold"/>
    <h:outputText value="#{viewParamJsonController.d}"/>
    
    <h:outputText value="Generic List" styleClass="bold"/>
    <h:outputText value="#{viewParamJsonController.list}"/>

    <h:outputText value="Generic Map" styleClass="bold"/>
    <h:outputText value="#{viewParamJsonController.map}"/>
    
    <h:outputText value="Non Generic" styleClass="bold"/>
    <h:outputText value="#{viewParamJsonController.fooNonGeneric}"/>

    <h:outputText value="Generic Simple" styleClass="bold"/>
    <h:outputText value="#{viewParamJsonController.fooGenericSimple}"/>

    <h:outputText value="Generic Complex" styleClass="bold"/>
    <h:outputText value="#{viewParamJsonController.fooGenericComplex}"/>
</h:panelGrid>
            
<p:inputText id="inputUrl" value="#{viewParamJsonController.generatedUrl}" style="width:100%"/>
            
<p:commandButton value="Open Link" type="button" style="margin-top:15px;"
                 onclick="openLinkInPopup()"/>

<h:outputScript id="jsonConverterJS" target="body">
    function openLinkInPopup() {
        var inputValue = $('#inputUrl').val();    
        if (inputValue !== null) {
            window.open(inputValue, 'Test JsonConverter with URL parameters',
                'width=auto,height=auto,resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,location=yes');
        }
    }       
</h:outputScript>
            
<h:outputStylesheet id="jsonConverterCSS">
    table .formColumn {
        padding-right: 10px;
        text-align: right;
        white-space: nowrap;
        width: 1%;
    }
    
    .bold {
        font-weight: bold;
    }
</h:outputStylesheet>
            
Components and more
Documentation pe:convertJson
Attributes (move mouse over the names to see data types)
Name Description
type Data type of the value object (optional). Any primitive type, array, non generic or generic type is supported. Data type is sometimes required to convert a value to a JSON representation. All data types should be fully qualified. Default is null.
PrimeFaces Extensions Showcase - © 2011-2021,PrimeFaces: 11.0.0,PrimeFaces Extensions: 11.0.0,JSF: Apache MyFaces JSF-2.3 Core API 2.3.9,Server: jetty/9.4.36.v20210114,Build time: 2021-12-10 14:32
occured!