Skip to content
79 changes: 79 additions & 0 deletions optional/gsJSON/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
### CMakeLists.txt --- JSON library
## Author: H.M. Verhelst
#
# The library is header-only, the extra code is downloaded in place of
# the source folder of G+Smo
#
######################################################################

## JSON extension
project(gsJSONExtension)

# Collect file names
aux_header_directory(${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_NAME}_HEADERS)

# Apply same configuration as G+Smo
include(gsConfig)

include(gsFetch)
gismo_fetch_directory(JSON
URL https://github.com/nlohmann/json/releases/download/v3.12.0/include.zip
DESTINATION external
)

# Turn off JSON's test
set(JSON_BuildTests OFF CACHE INTERNAL "")

set(JSON_INCLUDE_DIR ${gismo_SOURCE_DIR}/external/JSON/include/ CACHE INTERNAL "")
set (GISMO_INCLUDE_DIRS ${GISMO_INCLUDE_DIRS} ${JSON_INCLUDE_DIR}
CACHE INTERNAL "Gismo include directories" FORCE)

include_directories(${GISMO_INCLUDE_DIRS})

add_library(${PROJECT_NAME} INTERFACE)
target_sources(${PROJECT_NAME} INTERFACE $<BUILD_INTERFACE:${${PROJECT_NAME}_HEADERS}>)
add_dependencies(${PROJECT_NAME} JSON)

# Set standard properties for all G+Smo extensions
set_target_properties(${PROJECT_NAME} PROPERTIES
COMPILE_DEFINITIONS gismo_EXPORTS
POSITION_INDEPENDENT_CODE ON
LINKER_LANGUAGE CXX
#START Export all symbols from this extension
CXX_VISIBILITY_PRESET default
C_VISIBILITY_PRESET default
VISIBILITY_INLINES_HIDDEN 0
#END Export all symbols from this extension
FOLDER "G+Smo extensions"
)

# For gsXmlUtils.h
install(DIRECTORY ${CMAKE_SOURCE_DIR}/external/JSON/include/nlohmann
DESTINATION include/gismo
PATTERN "*/.svn" EXCLUDE
)

install(DIRECTORY ${PROJECT_SOURCE_DIR}
DESTINATION include/gismo
FILES_MATCHING PATTERN "gsJSON/*.h")


# add example files
#include_directories(${SPECTRA_INCLUDE_DIR})
aux_cpp_directory(${CMAKE_CURRENT_SOURCE_DIR}/examples FILES)
foreach(file ${FILES})
add_gismo_executable(${file})
get_filename_component(tarname ${file} NAME_WE) # name without extension
set_property(TEST ${tarname} PROPERTY LABELS "${PROJECT_NAME}")
set_target_properties(${tarname} PROPERTIES FOLDER "${PROJECT_NAME}")
# install the example executables (optionally)
install(TARGETS ${tarname} DESTINATION "${BIN_INSTALL_DIR}" COMPONENT exe OPTIONAL)
endforeach(file ${FILES})


# add unittests
aux_gs_cpp_directory(${PROJECT_SOURCE_DIR}/unittests unittests_SRCS)
set(gismo_UNITTESTS ${gismo_UNITTESTS} ${unittests_SRCS}
CACHE INTERNAL "gismo list of unittests")

set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin/)
128 changes: 128 additions & 0 deletions optional/gsJSON/examples/json_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/** @file

@brief

This file is part of the G+Smo library.

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.

Author(s):
*/

#include <gismo.h>
#include <gsJSON/gsJSON.h>

using namespace gismo;

int main(int argc, char *argv[])
{
std::string fileName;

gsCmdLine cmd("Tutorial for reading and writing JSON files in G+Smo.");
cmd.addString("f","file", "Input/output file name", fileName);
try { cmd.getValues(argc,argv); } catch (int rv) { return rv; }

gsInfo<<"=========================================================\n";
gsInfo<<"Creating a JSON file...\n";

// Create JSON writer
gsJSON j;

// Write integer
j["a"] = 2;
gsInfo<<"* Wrote an integer.\n";
// Write double
j["b"] = 3.0;
gsInfo<<"* Wrote a double.\n";
// Write string in a nested entry
j["c"]["d"] = "four";
gsInfo<<"* Wrote a string in a nested entry.\n";
// Write an array of strings
j["e"] = {"e1", "e2", "e3"};
gsInfo<<"* Wrote an array of strings.\n";

// Write a matrix
gsMatrix<> mat(2,2);
mat << 1, 2, 3, 4;
j["A"] = mat;
gsInfo<<"* Wrote a matrix.\n";

// Write a vector
gsVector<index_t> vec(3);
vec << 1, 2, 3;
j["v"] = vec;
gsInfo<<"* Wrote a vector.\n";

// Write a knot vector
gsKnotVector<> kv(0,1,0,2);
j["kv"] = kv;
gsInfo<<"* Wrote a knot vector.\n";

// Write a BSplineBasis
gsBSplineBasis<> basis(kv);
j["basis"] = basis;
gsInfo<<"* Wrote a BSplineBasis.\n";

// Write a TensorBSplineBasis
gsTensorBSplineBasis<2> tbasis(kv,kv);
j["tbasis"] = tbasis;
gsInfo<<"* Wrote a TensorBSplineBasis.\n";

// Write a TensorBSpline
gsMatrix<> coefs = gsMatrix<>::Random(tbasis.size(),2);
gsTensorBSpline<2> tgeom(tbasis, coefs);
j["tgeom"] = tgeom;
gsInfo<<"* Wrote a TensorBSpline.\n";

// Print the file
gsInfo<<"The contents of the JSON file are:\n"<<j<<"\n";

// Save the file
if (!fileName.empty())
{
gsInfo<<"Saving JSON file to "<<fileName<<"\n";
j.save(fileName);
}

gsInfo<<"=========================================================\n";
gsInfo<<"Construction of a JSON file from an option list.\n";
// Define a gsOptionList
gsOptionList opt;
opt.addInt("a", "", 2);
opt.addReal("b", "", 3.0);
gsInfo<<"The option list is:\n"<<opt<<"\n";

// Construct a JSON file from an option list
// gsJSON j2(opt);
j = gsJSON(opt); // This is the same as the above line, but using the gsJSON constructor directly
// Print the file
gsInfo<<"The contents of the JSON file constructed from an option list are:\n"<<j<<"\n";
// Obtain the option list from the JSON object
opt = j.get<gsOptionList>();
gsInfo<<"The option list obtained from the JSON object is:\n"<<opt<<"\n";

// Read the file written before
if (!fileName.empty())
{
gsInfo<<"=========================================================\n";
gsInfo<<"Reading the JSON file from "<<fileName<<"\n";
j = gsJSON(fileName);
// Print the knot vector
kv = j["kv"].get<gsKnotVector<>>();
gsInfo<<"* The knot vector read from the file is:\n"<<kv<<"\n";
// Print the basis
basis = j["basis"].get<gsBSplineBasis<>>();
gsInfo<<"* The BSplineBasis read from the file is:\n"<<basis<<"\n";
// Print the tensor basis
tbasis = j["tbasis"].get<gsTensorBSplineBasis<2>>();
gsInfo<<"* The TensorBSplineBasis read from the file is:\n"<<tbasis<<"\n";
// Print the tensor geometry
tgeom = j["tgeom"].get<gsTensorBSpline<2>>();
gsInfo<<"* The TensorBSpline read from the file is:\n"<<tgeom<<"\n";
}

return EXIT_SUCCESS;

}//main
Loading