Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Alvarez, Gonzalo
PsimagLite
Commits
3f5539f7
Commit
3f5539f7
authored
Jun 08, 2011
by
Alvarez, Gonzalo
Browse files
update json parser
parent
3591f9df
Changes
15
Hide whitespace changes
Inline
Side-by-side
drivers/Makefile
View file @
3f5539f7
LDFLAGS
=
-lm
-lpthread
-lblas
-llapack
CPPFLAGS
=
-Werror
-Wall
-I
../src
-I
../src/JSON/JsonParser
CPPFLAGS
=
-Werror
-Wall
-I
../src
-I
../src/JSON
-I
../src/JSON/JsonParser
CXX
=
g++
-g3
-DNDEBUG
all
:
$(EXENAME)
HEADERSH
=
../src/AkimaSpline.h ../src/Vector.h ../src/SampleCRSMatrix.h ../src/Minimizer.h
...
...
drivers/jsonExample.cpp
View file @
3f5539f7
...
...
@@ -9,7 +9,7 @@
*
*/
#include "Json
Pars
er.h"
#include "Json
Read
er.h"
#include "DefaultContext.h"
#include <wchar.h>
#include <iostream>
...
...
@@ -23,16 +23,24 @@ int main(int argc,char *argv[]) {
}
std
::
string
fileName
(
argv
[
1
]);
std
::
wifstream
file
;
/*
std::wifstream file;
file
.
open
(
fileName
.
c_str
());
file.open(fileName.c_str());
*/
JsonParser
::
JsonParser
<
JsonParser
::
DefaultContext
>
parser
;
parser
.
filename
=
fileName
;
while
(
parser
.
parseChar
(
file
));
// JsonParser::JsonParser<JsonParser::DefaultContext> parser;
// parser.filename = fileName;
// while(parser.parseChar(file));
dca
::
JsonReader
reader
(
fileName
);
int
totalBins
=
0
;
totalBins
<=
reader
[
"programSpecific"
][
"DCA"
][
"control"
][
"totalBins"
];
std
::
wcout
<<
" totalBins = "
<<
totalBins
<<
"
\n
"
;
int
numPointInBath
=
0
;
numPointInBath
<=
reader
.
searchFor
(
"numPointsInBath"
);
std
::
wcout
<<
" numPointsInBath = "
<<
numPointInBath
<<
"
\n
"
;
std
::
wcout
<<
parser
.
ctx
;
std
::
wcout
<<
"
\n
"
;
//
std::wcout << parser.ctx;
return
0
;
}
...
...
src/JSON/JSN_Writer.h
View file @
3f5539f7
...
...
@@ -10,20 +10,285 @@
#ifndef dca_JSN_Writer_H
#define dca_JSN_Writer_H
#include "VectorLike.h"
namespace
dca
{
using
namespace
psimag
::
VectorLike
;
template
<
typename
T
>
class
PrintArrayWidth
{
public
:
enum
{
value
=
7
};
};
template
<
>
class
PrintArrayWidth
<
double
>
{
public
:
enum
{
value
=
13
};
};
template
<
template
<
typename
,
typename
>
class
MapType
=
std
::
map
>
class
JSN
{
public:
int
width
;
bool
doNothing
;
typedef
enum
{
First
,
Middle
,
Last
}
Position
;
typedef
JSN
<
MapType
>
ThisType
;
//======================================================================
JSN
(
bool
action
=
true
)
:
width
(
13
),
doNothing
(
action
)
JSN
(
size_t
offset_
=
0
)
:
offset
(
offset_
),
precis
(
10
),
printedFirstLine
(
false
)
{}
//======================================================================
class
KeyReference
{
public:
JSN
&
writer
;
std
::
string
key
;
KeyReference
(
std
::
string
k
,
JSN
&
w
)
:
writer
(
w
),
key
(
k
)
{}
template
<
typename
T
>
void
operator
=
(
const
T
&
val
)
{
writer
.
add
(
key
,
val
);
}
operator
JSN
&
()
{
return
writer
;
}
KeyReference
operator
[](
std
::
string
subKey
)
{
return
writer
.
getComponentWriter
(
key
)[
subKey
];
}
};
//======================================================================
KeyReference
operator
[](
std
::
string
key
)
{
return
KeyReference
(
key
,
*
this
);
}
//======================================================================
ThisType
&
getComponentWriter
(
std
::
string
key
)
{
return
writers
[
key
];
}
//======================================================================
mutable
int
offset
;
int
precis
;
mutable
bool
printedFirstLine
;
MapType
<
std
::
string
,
bool
>
bools
;
MapType
<
std
::
string
,
std
::
string
>
strings
;
MapType
<
std
::
string
,
double
>
numbers
;
MapType
<
std
::
string
,
std
::
vector
<
double
>
>
vectors
;
MapType
<
std
::
string
,
std
::
vector
<
int
>
>
intvectors
;
MapType
<
std
::
string
,
std
::
vector
<
size_t
>
>
sizetvectors
;
MapType
<
std
::
string
,
std
::
vector
<
std
::
string
>
>
stringLists
;
MapType
<
std
::
string
,
const
psimag
::
Matrix
<
int
>*>
intMatrices
;
MapType
<
std
::
string
,
const
psimag
::
Matrix
<
double
>*>
dblMatrices
;
MapType
<
std
::
string
,
const
psimag
::
Matrix
<
std
::
complex
<
double
>
>*>
cMatrices
;
MapType
<
std
::
string
,
ThisType
>
writers
;
//======================================================================
void
add
(
std
::
string
key
,
const
bool
&
b
)
{
bools
[
key
]
=
b
;
}
void
add
(
std
::
string
key
,
const
char
*
str
)
{
strings
[
key
]
=
str
;
}
void
add
(
std
::
string
key
,
const
std
::
string
&
str
)
{
strings
[
key
]
=
str
;
}
void
add
(
std
::
string
key
,
const
size_t
&
val
)
{
numbers
[
key
]
=
val
;
}
void
add
(
std
::
string
key
,
const
int
&
val
)
{
numbers
[
key
]
=
val
;
}
void
add
(
std
::
string
key
,
const
double
&
val
)
{
numbers
[
key
]
=
val
;
}
void
add
(
std
::
string
key
,
const
std
::
vector
<
double
>&
vals
)
{
vectors
[
key
]
=
vals
;
}
void
add
(
std
::
string
key
,
const
std
::
vector
<
int
>&
vals
)
{
intvectors
[
key
]
=
vals
;
}
void
add
(
std
::
string
key
,
const
std
::
vector
<
size_t
>&
vals
)
{
sizetvectors
[
key
]
=
vals
;
}
void
add
(
std
::
string
key
,
const
psimag
::
Matrix
<
int
>&
mat
)
{
intMatrices
[
key
]
=
&
mat
;
}
void
add
(
std
::
string
key
,
const
psimag
::
Matrix
<
double
>&
mat
)
{
dblMatrices
[
key
]
=
&
mat
;
}
//======================================================================
template
<
class
T
>
void
add
(
std
::
string
key
,
const
T
&
obj
){
obj
.
toJSN
(
getComponentWriter
(
key
));
}
//======================================================================
template
<
typename
T
>
void
add
(
std
::
string
key
,
const
std
::
map
<
std
::
string
,
T
>&
map
)
{
typedef
typename
std
::
map
<
std
::
string
,
T
>::
const_iterator
itr
;
ThisType
&
writer
=
writers
[
key
];
for
(
itr
i
=
map
.
begin
();
i
!=
map
.
end
();
i
++
)
writer
.
add
(
i
->
first
,
i
->
second
);
}
//======================================================================
template
<
typename
ValType
>
static
int
maxKeyWidth
(
const
MapType
<
std
::
string
,
ValType
>&
map
)
{
size_t
result
=
0
;
typedef
typename
MapType
<
std
::
string
,
ValType
>::
const_iterator
itr
;
for
(
itr
i
=
map
.
begin
();
i
!=
map
.
end
();
i
++
)
if
(
i
->
first
.
length
()
>
result
)
result
=
i
->
first
.
length
();
return
result
+
2
;
}
//======================================================================
int
maxKeyWidth
()
const
{
int
result
=
0
;
int
maxKeyLen
=
0
;
maxKeyLen
=
maxKeyWidth
(
strings
);
if
(
maxKeyLen
>
result
)
result
=
maxKeyLen
;
maxKeyLen
=
maxKeyWidth
(
bools
);
if
(
maxKeyLen
>
result
)
result
=
maxKeyLen
;
maxKeyLen
=
maxKeyWidth
(
numbers
);
if
(
maxKeyLen
>
result
)
result
=
maxKeyLen
;
maxKeyLen
=
maxKeyWidth
(
vectors
);
if
(
maxKeyLen
>
result
)
result
=
maxKeyLen
;
maxKeyLen
=
maxKeyWidth
(
intvectors
);
if
(
maxKeyLen
>
result
)
result
=
maxKeyLen
;
maxKeyLen
=
maxKeyWidth
(
sizetvectors
);
if
(
maxKeyLen
>
result
)
result
=
maxKeyLen
;
maxKeyLen
=
maxKeyWidth
(
stringLists
);
if
(
maxKeyLen
>
result
)
result
=
maxKeyLen
;
maxKeyLen
=
maxKeyWidth
(
writers
);
if
(
maxKeyLen
>
result
)
result
=
maxKeyLen
;
maxKeyLen
=
maxKeyWidth
(
intMatrices
);
if
(
maxKeyLen
>
result
)
result
=
maxKeyLen
;
maxKeyLen
=
maxKeyWidth
(
dblMatrices
);
if
(
maxKeyLen
>
result
)
result
=
maxKeyLen
;
maxKeyLen
=
maxKeyWidth
(
cMatrices
);
if
(
maxKeyLen
>
result
)
result
=
maxKeyLen
;
return
result
;
}
//======================================================================
template
<
typename
ValType
>
void
printLines
(
std
::
ostream
&
os
,
const
MapType
<
std
::
string
,
ValType
>&
map
,
int
offset
,
int
keyWidth
,
Position
position
=
Middle
)
const
{
typedef
typename
MapType
<
std
::
string
,
ValType
>::
const_iterator
itr
;
for
(
itr
i
=
map
.
begin
();
i
!=
map
.
end
();
i
++
)
{
printLine
(
os
,
i
->
first
,
i
->
second
,
offset
,
keyWidth
,
printedFirstLine
);
printedFirstLine
=
true
;
}
}
//======================================================================
template
<
typename
ValType
>
static
void
printLine
(
std
::
ostream
&
os
,
const
std
::
string
&
key
,
const
ValType
&
val
,
int
offset
,
int
keyWidth
,
bool
printedFirstLine_
,
bool
printVal
=
true
)
{
if
(
printedFirstLine_
)
os
<<
",
\n
"
<<
std
::
string
(
offset
,
' '
)
;
os
<<
std
::
setw
(
keyWidth
)
<<
std
::
left
<<
quoted
(
key
);
os
<<
" : "
;
if
(
printVal
)
valString
(
os
,
offset
,
keyWidth
,
val
)
;
}
//======================================================================
//======================================================================
template
<
typename
T
>
static
void
valString
(
std
::
ostream
&
os
,
int
offset
,
int
keyWidth
,
const
T
&
val
)
{
os
<<
val
;
}
template
<
typename
T
>
static
void
valString
(
std
::
ostream
&
os
,
int
offset
,
int
keyWidth
,
const
psimag
::
Matrix
<
T
>*
val
)
{
const
psimag
::
Matrix
<
T
>&
matrix
(
*
val
);
int
kWidth
=
6
;
int
newOffset
=
offset
+
keyWidth
+
4
;
os
<<
"{"
;
printLine
(
os
,
"rows"
,
matrix
.
n_row
(),
newOffset
,
kWidth
,
false
);
printLine
(
os
,
"cols"
,
matrix
.
n_col
(),
newOffset
,
kWidth
,
true
);
printLine
(
os
,
"data"
,
0
,
newOffset
,
kWidth
,
true
,
false
);
psimag
::
MatrixLike
::
printArray
(
matrix
,
os
,
PrintArrayWidth
<
T
>::
value
,
newOffset
+
8
);
os
<<
"}"
;
}
static
void
valString
(
std
::
ostream
&
os
,
int
offset
,
int
keyWidth
,
const
std
::
string
&
val
)
{
os
<<
quoted
(
val
);
}
static
void
valString
(
std
::
ostream
&
os
,
int
offset
,
int
keyWidth
,
const
ThisType
&
val
)
{
val
.
print
(
os
,
offset
+
keyWidth
+
4
);
}
template
<
typename
T
>
static
void
valString
(
std
::
ostream
&
os
,
int
offset
,
int
keyWidth
,
const
std
::
vector
<
T
>&
val
)
{
size_t
width
=
maxElementStringWidth
(
val
);
printList
(
val
,
os
,
','
,
width
);
}
//======================================================================
void
print
(
std
::
ostream
&
os
,
int
off
=
1
,
bool
printLastBrace
=
true
)
const
{
offset
=
off
;
printedFirstLine
=
false
;
int
keyWidth
=
maxKeyWidth
();
os
<<
"{"
;
printLines
(
os
,
strings
,
offset
,
keyWidth
);
printLines
(
os
,
bools
,
offset
,
keyWidth
);
printLines
(
os
,
numbers
,
offset
,
keyWidth
);
printLines
(
os
,
vectors
,
offset
,
keyWidth
);
printLines
(
os
,
sizetvectors
,
offset
,
keyWidth
);
printLines
(
os
,
intvectors
,
offset
,
keyWidth
);
printLines
(
os
,
stringLists
,
offset
,
keyWidth
);
printLines
(
os
,
intMatrices
,
offset
,
keyWidth
);
printLines
(
os
,
dblMatrices
,
offset
,
keyWidth
);
printLines
(
os
,
cMatrices
,
offset
,
keyWidth
);
printLines
(
os
,
writers
,
offset
,
keyWidth
);
if
(
printLastBrace
)
os
<<
"}"
;
}
//======================================================================
static
std
::
string
toString
(
const
std
::
map
<
std
::
string
,
std
::
string
>&
map
)
{
std
::
ostringstream
buff
;
buff
<<
"{"
;
std
::
map
<
std
::
string
,
std
::
string
>::
const_iterator
itr
;
...
...
@@ -37,6 +302,7 @@ namespace dca {
}
template
<
typename
T
>
static
std
::
string
toString
(
const
std
::
map
<
std
::
string
,
T
>&
map
)
{
typedef
std
::
map
<
std
::
string
,
T
>
MapType
;
typedef
typename
MapType
::
const_iterator
CITR
;
...
...
@@ -75,7 +341,7 @@ namespace dca {
void
printVector
(
std
::
vector
<
T
>
vec
,
std
::
string
title
,
std
::
ostream
&
os
)
{
os
.
precision
(
width
);
os
.
precision
(
precis
);
os
<<
std
::
fixed
;
// scientific;
os
<<
"{ "
<<
"
\'
tile
\'
:
\'
"
<<
title
<<
"
\'
,
\n
"
...
...
@@ -84,100 +350,21 @@ namespace dca {
os
<<
"
\'
data
\'
: "
;
os
<<
"array([["
;
for
(
size_t
j
=
0
;
j
<
vec
.
size
();
j
++
)
os
<<
" "
<<
std
::
setw
(
width
)
<<
vec
[
j
]
<<
",
\n
"
;
os
<<
" "
<<
std
::
setw
(
precis
)
<<
vec
[
j
]
<<
",
\n
"
;
os
<<
"]])
\n
"
;
os
<<
"}"
;
}
template
<
typename
ObjType
>
void
writeToFile
(
std
::
string
fileName
,
std
::
string
objName
,
const
ObjType
&
obj
)
{
std
::
ofstream
file
;
std
::
string
vtkFileName
(
fileName
);
vtkFileName
+=
".vts"
;
file
.
open
(
fileName
.
c_str
());
obj
.
toJSN
(
file
,
objName
,
width
);
//obj.writeVTK(vtkFileName);
file
.
close
();
}
template
<
typename
T
>
void
writeToFile
(
std
::
string
fileName
,
std
::
string
objName
,
const
std
::
vector
<
T
>&
obj
)
{
std
::
ofstream
file
;
file
.
open
(
fileName
.
c_str
());
printVector
(
obj
,
objName
,
file
);
file
.
close
();
}
template
<
typename
Type
>
void
write
(
const
std
::
vector
<
Type
>&
vec
,
std
::
string
objName
,
std
::
string
collectionName
,
int
index
,
std
::
string
parentName
)
{
if
(
doNothing
)
return
;
std
::
ostringstream
fileNameBuff
;
fileNameBuff
<<
"debug/"
<<
collectionName
<<
"/"
<<
index
<<
"/"
<<
parentName
<<
"/"
<<
objName
<<
".py"
;
writeToFile
(
fileNameBuff
.
str
(),
objName
,
vec
);
}
template
<
typename
ObjType
>
void
write
(
const
ObjType
&
obj
,
std
::
string
fileName
,
std
::
string
title
=
"jsn object"
)
{
if
(
doNothing
)
return
;
writeToFile
(
fileName
,
title
,
obj
);
}
template
<
typename
ObjType
>
void
write
(
const
ObjType
&
obj
,
std
::
string
objName
,
std
::
string
collectionName
,
int
index
,
std
::
string
parentName
)
{
if
(
doNothing
)
return
;
std
::
ostringstream
fileNameBuff
;
fileNameBuff
<<
"debug/"
<<
collectionName
<<
"/"
<<
index
<<
"/"
<<
parentName
<<
"/"
<<
objName
<<
".py"
;
writeToFile
(
fileNameBuff
.
str
(),
objName
,
obj
);
}
template
<
typename
ObjType
>
void
write
(
const
ObjType
&
obj
,
std
::
string
objName
,
std
::
string
collectionName
,
int
index
,
std
::
string
parentName
,
std
::
string
subCollectionName
,
int
index2
)
{
if
(
doNothing
)
return
;
std
::
ostringstream
fileNameBuff
;
fileNameBuff
<<
"debug/"
<<
collectionName
<<
"/"
<<
index
<<
"/"
<<
parentName
<<
"/"
<<
subCollectionName
<<
"/"
<<
index2
<<
"/"
<<
objName
<<
".py"
;
writeToFile
(
fileNameBuff
.
str
(),
objName
,
obj
);
}
template
<
typename
ObjType
>
void
write
(
const
ObjType
&
obj
,
std
::
string
objName
,
std
::
string
collectionName
,
int
index
,
std
::
string
parentName
,
std
::
string
subCollectionName
)
{
if
(
doNothing
)
return
;
std
::
ostringstream
fileNameBuff
;
fileNameBuff
<<
"debug/"
<<
collectionName
<<
"/"
<<
index
<<
"/"
<<
parentName
<<
"/"
<<
subCollectionName
<<
"/"
<<
objName
<<
".py"
;
std
::
string
fileName
(
fileNameBuff
.
str
());
writeToFile
(
fileNameBuff
.
str
(),
objName
,
obj
);
//======================================================================
static
std
::
string
quoted
(
std
::
string
str
)
{
std
::
ostringstream
result
;
result
<<
"
\"
"
<<
str
<<
"
\"
"
;
return
result
.
str
();
}
//======================================================================
};
}
// end namespace DCA
...
...
src/JSON/JsonParser/CharacterMapper.h
View file @
3f5539f7
...
...
@@ -14,7 +14,7 @@
#include <wchar.h>
#include <string>
#include <stdexcept>
namespace
JsonParser
{
class
CharacterMapper
{
...
...
src/JSON/JsonParser/JsonParser.h
View file @
3f5539f7
...
...
@@ -91,7 +91,7 @@ namespace JsonParser {
while
(
isWhiteSpace
(
nextClass
))
{
if
(
inputStream
.
eof
())
{
nextChar
=
C_
EOF
;
// <-- changed by G.A.
nextChar
=
EOF
;
nextClass
=
C_EOF
;
return
result
;
}
...
...
@@ -99,7 +99,7 @@ namespace JsonParser {
nextChar
=
inputStream
.
get
();
if
(
nextChar
==
static_cast
<
wchar_t
>
(
WEOF
))
{
nextChar
=
C_
EOF
;
//<-- changed by G.A.
nextChar
=
EOF
;
nextClass
=
C_EOF
;
return
result
;
}
...
...
src/JSON/JsonParser/Makefile
0 → 100644
View file @
3f5539f7
#
SHELL
=
/bin/bash
SRC_DIR
=
.
INCLUDES
=
-I
.
-I
$(SRC_DIR)
EXENAME
=
parseTest
CC
=
g++
cc
=
gcc
MatrixBoundsChecking
=
-DNDEBUG
FLAGS
=
$(MatrixBoundsChecking)
-g
-Wall
-Werror
#Note that mjson does not compile with -Werror -Wall, this is
# a temporary solution :
FLAGSC
=
$(MatrixBoundsChecking)
OBJECTS
=
main.o
all
:
clean showEnv $(OBJECTS) $(EXENAME)
run
:
all
./parseTest small.jsn
showEnv
:
@
echo
----------------------------------------------------------------
Env Info:
@
echo
------------------------------------------------
Compiler Version
$(CC)
--version
@
echo
----------------------------------------------------------------
@
echo
main.o
:
main.cpp
$(CC)
$(INCLUDES)
$(FLAGS)
-c
main.cpp
main.s
:
main.cpp
$(CC)
-S
$(INCLUDES)
$(FLAGS)
-c
main.cpp
$(EXENAME)
:
$(OBJECTS)
$(CC)
$(INCLUDES)
$(FLAGS)
$(OBJECTS)
-o
$(EXENAME)
-lblas
cp
$(EXENAME)
$(EXENAME)
_stripped
strip
$(EXENAME)
_stripped
clean
:
rm
-f
$(EXENAME)
*
.o
########################End of Makefile #################################
src/JSON/JsonParser/MatrixParser.h
View file @
3f5539f7
...
...
@@ -17,6 +17,7 @@
#include <map>
#include <vector>
#include "CharacterMapper.h"
#include "MatrixLike.h"
namespace
JsonParser
{
...
...
@@ -103,7 +104,7 @@ namespace JsonParser {
FieldType
&
value
(
mat
(
datarow
,
datacol
));
consume
(
value
);
//
std::cout << "mat(" << datarow << "," << datacol << ")= " << value << "\n";
//
std::cout << "mat(" << datarow << "," << datacol << ")= " << value << "\n";
datacol
++
;
consume
(
L','
);
...
...
@@ -111,6 +112,8 @@ namespace JsonParser {
datarow
++
;
consume
(
L','
);
}
// std::cout << "consume done\n";
// psimag::MatrixLike::printList(mat,std::cout);
}
//======================================================================
...
...
src/JSON/JsonParser/Whatever.h
View file @
3f5539f7
...
...
@@ -436,6 +436,12 @@ namespace JsonParser {
//======================================================================
// operator bool() { bool result <= *this; return result; }
// operator int() { int result <= *this; return result; }
// operator double() { double result <= *this; return result; }
// operator float() { double result <= *this; return static_cast<float>(result); }
// operator std::string() { std::string result <= *this; return result; }
};
template
<
typename
T
>
class
TYPE
{
public
:
static
Whatever
::
WhateverType
to
()
{
return
Whatever
::
WHATEVER_UNKNOWN
;
}
};
...
...
src/JSON/JsonParser/main.cpp
0 → 100644
View file @
3f5539f7
//-*-C++-*-
/** \ingroup main.JsonParser */
/*@{*/
/*! \file main.c
*
*
*
*/
#ifndef JsonParser_MAIN_H
#define JsonParser_MAIN_H
#include "JsonParser.h"
#include "DefaultContext.h"
#include <wchar.h>
#include <iostream>
#include <fstream>
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
<
2
)
{
std
::
cout
<<
"Usage: "
<<
argv
[
0
]
<<
" inputFileName
\n
"
;