1

有没有办法在 smooks 中通过值替换来做 json-to-json?

在 xml 中执行此操作非常简单,例如

xml文件:

<order id='332'>
<header>
    <customer number="123">Joe</customer>
</header>
<order-items>
    <order-item id='1'>
        <product>1</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id='2'>
        <product>2</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id='3'>
        <product>3</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id='4'>
        <product>4</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id='5'>
        <product>5</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
</order-items>

烟雾配置:

<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                      xmlns:core="http://www.milyn.org/xsd/smooks/smooks-    core-1.3.xsd"
                      xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">
    <resource-config selector="order-item">
        <resource>org.milyn.delivery.DomModelCreator</resource>
    </resource-config>

<ftl:freemarker applyOnElement="order-item/product">
    <ftl:template><!--<#t>
    <#t><#if .vars["order-item"].product?number gt 3><product>Car</product>
    <#t><#else><product>Bike</product></#if>--></ftl:template>
</ftl:freemarker>

</smooks-resource-list>

输出:

<order id="332">
<header>
    <customer number="123">Joe</customer>
</header>
<order-items>
    <order-item id="1">
        <product>Bike</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id="2">
        <product>Bike</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id="3">
        <product>Bike</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id="4">
        <product>Car</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
    <order-item id="5">
        <product>Car</product>
        <quantity>2</quantity>
        <price>8.80</price>
    </order-item>
</order-items>

完美的。

现在,我想对 Json 做一些类似的事情,通过大多数未触及的行并只修改一些值:

JSON:

{
"header": {
    "order id": "456",
    "date&time" : "Wed Nov 15 13:45:28 EST 2006",
    "customer": {
        "number": "123",
        "name": "Joe"
    }
},
"order item": [
    {
        "product id": "1",
        "quantity": "2",
        "price": "8.80"
    },
    {
        "product id": "2",
        "quantity": "2",
        "price": "8.80"
    },
    {
        "product id": "3",
        "quantity": "2",
        "price": "8.80"
    },
    {
        "product id": "4",
        "quantity": "2",
        "price": "8.80"
    },
    {
        "product id": "5",
        "quantity": "2",
        "price": "8.80"
    }
]}

烟雾配置:

<?xml version="1.0"?>
<smooks-resource-list
     xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
     xmlns:json="http://www.milyn.org/xsd/smooks/json-1.1.xsd"
     xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.3.xsd"
     xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">

    <json:reader rootName="order" keyWhitspaceReplacement="_">
        <json:keyMap>
            <json:key from="date&amp;time" to="date_and_time" />
        </json:keyMap>
    </json:reader>

     <core:filterSettings type="SAX" defaultSerialization="true" />

    <resource-config selector="order_item/element">
       <resource>org.milyn.delivery.DomModelCreator</resource>
    </resource-config>

    <ftl:freemarker applyOnElement="order_item/element/product_id">
       <ftl:template><!--
       <#t><#if element.product_id?number gt 3>{"product_id":"Car"}
       <#t><#else>{"product_id":"Bike"}</#if>--></ftl:template>
    </ftl:freemarker>

</smooks-resource-list>

输出:

<order>
  <header>
    <order_id>456</order_id>
    <date_and_time>Wed Nov 15 13:45:28 EST 2006</date_and_time>
    <customer>
      <number>123</number>
      <name>Joe</name>
    </customer>
  </header>
  <order_item>
    <element>
      {"product_id":"Bike"}
      <quantity>2</quantity>
      <price>8.80</price>
    </element>
    <element>
      {"product_id":"Bike"}
      <quantity>2</quantity>
      <price>8.80</price>
    </element>
    <element>
      {"product_id":"Bike"}
      <quantity>2</quantity>
      <price>8.80</price>
    </element>
    <element>
       {"product_id":"Car"}
       <quantity>2</quantity>
       <price>8.80</price>
    </element>
    <element>
      {"product_id":"Car"}
      <quantity>2</quantity>
      <price>8.80</price>
      </element>
      </order_item>
</order>

我知道我可以关闭默认序列化,并且只传递 freemarker 中的行,但这不是我想要的,即

{
  "header": {
    "order id": "456",
    "date&time" : "Wed Nov 15 13:45:28 EST 2006",
    "customer": {
      "number": "123",
      "name": "Joe"
    }
  },
  "order item": [
  {
    "product id": "Bike",
    "quantity": "2",
    "price": "8.80"
  },
  {
    "product id": "Bike",
    "quantity": "2",
    "price": "8.80"
  },
  {
    "product id": "Bike",
    "quantity": "2",
    "price": "8.80"
  },
  {
    "product id": "Car",
    "quantity": "2",
    "price": "8.80"
  },
  {
    "product id": "Car",
    "quantity": "2",
    "price": "8.80"
  }
]}

有什么方法可以将默认输出序列化切换为 JSON,或者有任何类似的方式来输出 JSON,而无需包含每个未更改的标签?

提前致谢。

4

0 回答 0